我希望显示带有自然日志的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))
为什么情节是一样的?
谢谢
答案 0 :(得分:1)
线是相同的,因为将线性模型中的特征乘以常数不会改变拟合,系数只是除以相同的常数。 &#34;基地的变化&#34;公式告诉我们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)
,因为在此答案开头引用了基本公式的更改。