ggplot2,使用log2或log10拟合数据不会影响绘图

詹卢卡

我想要显示带有自然日志的geom_smooth,此代码可以正常工作:

    df <- iris
iris_logplot <- ggplot(df, aes(Sepal.Length, Sepal.Width, colour = Species))

iris_logplot + stat_summary(fun.y =median, geom = "point") + stat_summary(fun.data = mean_cl_boot, aes(group = Species), geom = "errorbar", width = 0.2) + 
  geom_smooth(method="lm", formula=y~log(x)) 

现在,我想显示一个geom_smooth,其日志的底数为2,并应用以下代码:

df <- iris
iris_logplot <- ggplot(df, aes(Sepal.Length, Sepal.Width, colour = Species))

iris_logplot + stat_summary(fun.y =median, geom = "point") +
  stat_summary(fun.data = mean_cl_boot, aes(group = Species), geom = "errorbar", width = 0.2) + geom_smooth(method="lm", formula=y~log2(x)) 

为什么地块相同?

谢谢

格雷戈尔·托马斯(Gregor Thomas)

线是相同的,因为将线性模型中的特征乘以常数不会改变拟合,系数只是被相同的常数除。“基数变化”公式告诉我们log_b(x) = log_a(x) / log_a(b)

我们可以通过检查模型来验证这一点:

m_log_e = lm(Sepal.Width ~ log(Sepal.Length) * Species, data = iris)
m_log_2 = lm(Sepal.Width ~ log2(Sepal.Length) * Species, data = iris)

summary(m_log_e)
# Call:
# lm(formula = Sepal.Width ~ log(Sepal.Length) * Species, data = iris)
# 
# Residuals:
#      Min       1Q   Median       3Q      Max 
# -0.71398 -0.15310 -0.00419  0.16595  0.60237 
# 
# Coefficients:
#                                     Estimate Std. Error t value Pr(>|t|)    
# (Intercept)                          -2.9663     0.8872  -3.343 0.001055 ** 
# log(Sepal.Length)                     3.9760     0.5512   7.214 2.86e-11 ***
# Speciesversicolor                     2.3355     1.1899   1.963 0.051595 .  
# Speciesvirginica                      3.0464     1.1639   2.617 0.009807 ** 
# log(Sepal.Length):Speciesversicolor  -2.0626     0.7087  -2.910 0.004186 ** 
# log(Sepal.Length):Speciesvirginica   -2.4373     0.6811  -3.579 0.000471 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.272 on 144 degrees of freedom
# Multiple R-squared:  0.6237,  Adjusted R-squared:  0.6106 
# F-statistic: 47.73 on 5 and 144 DF,  p-value: < 2.2e-16

summary(m_log_2)
# Call:
# lm(formula = Sepal.Width ~ log2(Sepal.Length) * Species, data = iris)
# 
# Residuals:
#      Min       1Q   Median       3Q      Max 
# -0.71398 -0.15310 -0.00419  0.16595  0.60237 
# 
# Coefficients:
#                                      Estimate Std. Error t value Pr(>|t|)    
# (Intercept)                           -2.9663     0.8872  -3.343 0.001055 ** 
# log2(Sepal.Length)                     2.7560     0.3820   7.214 2.86e-11 ***
# Speciesversicolor                      2.3355     1.1899   1.963 0.051595 .  
# Speciesvirginica                       3.0464     1.1639   2.617 0.009807 ** 
# log2(Sepal.Length):Speciesversicolor  -1.4297     0.4913  -2.910 0.004186 ** 
# log2(Sepal.Length):Speciesvirginica   -1.6894     0.4721  -3.579 0.000471 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
# Residual standard error: 0.272 on 144 degrees of freedom
# Multiple R-squared:  0.6237,  Adjusted R-squared:  0.6106 
# F-statistic: 47.73 on 5 and 144 DF,  p-value: < 2.2e-16

比较这些摘要,您可以使自己确信拟合是相同的-残差相同,统计量相同,截距相同,唯一的不同是项的系数,包括Sepal.Length我们可以将系数相除:

coef(m_log_e) / coef(m_log_2)
#                         (Intercept)                   log(Sepal.Length)                   Speciesversicolor                    Speciesvirginica 
#                            1.000000                            1.442695                            1.000000                            1.000000 
# log(Sepal.Length):Speciesversicolor  log(Sepal.Length):Speciesvirginica 
#                            1.442695                            1.442695 

并看到涉及的术语Sepal.Length是固定比例的。那比率是多少?

1 / log(2)
# [1] 1.442695

1 /log(2),因为此答案开头引用的基本公式已更改。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章