r - 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))
为什么情节是一样的?
谢谢
解决方案
这些线是相同的,因为将线性模型中的特征乘以常数不会改变拟合,系数只是除以相同的常数。“基数变化”公式告诉我们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)
,因为这个答案开头引用的基本公式发生了变化。
推荐阅读
- ansible - 如果所有主机都针对,则限制任务
- reactjs - 数据表问题(显示值但无法过滤项目)而值显示在表中,因此顶部没有项目可显示
- go - Go 语言类型推断何时发生?
- delphi - 使用 TSVGIconImage 将 SVG 导出为位图时设置透明度颜色
- javascript - 如何检测后退按钮并在按下时重定向到另一个页面?
- java - 不知道如何将“Recipe4”转换为 edu.ncsu.csc326.coffeemaker.Recipe
- sql - 我可以在 teradata 中使用 union all 语句和 insert into 语句吗?
- lstm - 简单的 RNN 使用 Glove 的准确率低于没有使用 Glove 的准确率
- python - 如何在运行单元测试python时等待异步函数给出响应
- c# - 如何在安装了 .net 5 sdk 的情况下运行 net 3.1 测试