如何获得带有日志变量的lm的正确截距?

时间:2019-05-01 09:39:20

标签: r ggplot2 lm

以下是一些示例数据:

df <- data.frame('x' = c(230890.00, 71023.25, 296.00, 217530.00, 736550.00, 285529.50, 15210.25, 20317.00, 25157.75,
                     1272.50, 7817.75, 61297.50, 9085.00, 16047.25, 10008.75, 2438.75),
                 'y' = c(2.030330e-11, 1.016549e-05, 9.308836e-17, 5.587085e-02, 1.122631e+01, 1.104037e-06, 1.609258e-04,
                     1.653132e-04, 1.849783e-04, 6.611519e-15, 6.096827e-14, 1.197042e-09, 7.425932e-08, 2.398086e-05,
                     5.677278e-13, 2.704369e-15))

我想将此数据绘制为散点图,在对数轴上用线表示线性回归,并用标签表示回归方程和R ^ 2值。

为此,我需要输入代码:

ggplot(df, aes(x = x, y = y)) +
  geom_point() +
  geom_smooth(method = 'lm', se = F, col = 'grey', linetype = 'dashed') +
  stat_poly_eq(formula = df$y ~ df$x, 
               aes(label = paste(..eq.label.., ..rr.label.., sep = '*plain(\',\')~')), 
               parse = T ) +
  scale_y_continuous(trans = 'log10',
                 breaks = trans_breaks('log10', function(x) 10^x),
                 labels = trans_format('log10', math_format(10^.x))) +
  scale_x_continuous(trans = 'log10',
                 breaks = trans_breaks('log10', function(x) 10^x),
                 labels = trans_format('log10', math_format(10^.x))) +
  annotation_logticks() +
  theme(
    panel.background = element_blank(),
    axis.text = element_text(colour = 'black', size = 11),
    axis.line = element_line(colour = 'black'),
    axis.ticks = element_line(colour = 'black'),
    legend.position = 'none'
  )

哪个给我这个输出:

enter image description here

但是,如您所见,截距是错误的。我假设这是因为我没有在stat_poly_eq的公式中对x和y进行对数变换,但是如果我对它们进行了对数变换,则它们将不会与图形轴处于相同比例(截距变为-26)。

如何获取公式以显示截距在10 ^ -16左右?

0 个答案:

没有答案