我正在尝试将某些模型拟合到某些数据中,并且得到的模型预测了合理的值,并且图表看起来是正确的。但是当提取系数并分别绘制函数时,它们毫无意义!我显然做错了,所以请有人告诉我错误在哪里?
数据:
dput(distcur)
structure(list(id1 = c(1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6, 1.6
), range = c(-39.898125, -21.448125, -11.07, -3.22875, 3.776484375,
12.309609375, 22.399453125, 39.235078125), meanrat = c(20.2496,
17.7504273504274, 12.76875, 2.475, -1.4295652173913, -3.9603305785124,
-14.7008547008547, -19.7366666666667)), .Names = c("id1", "range",
"meanrat"), row.names = 9:16, class = "data.frame")
library(ggplot2)
id = 1.6
degree = 3
press_x <- seq(min(distcur$range), max(distcur$range), length = 500)
moddist3b <- lm(meanrat ~ poly(range, degree), distcur)
valsdist = data.frame(predict(moddist3b, data.frame(range = press_x)))
colnames(valsdist) = "pred"
valsdist$id1 = id
allvals = cbind(valsdist, press_x)
summary(moddist3b)
#test plot
pdf(paste("mod-",measure,id ))
TITLE = paste("Distance ID: ", id, "Model = line, Points = exp1")
p = ggplot(allvals, aes(x=press_x, y=pred, colour=factor(id1))) +
geom_line() +
geom_point(data=distcur, aes(shape=factor(id1), x = range, y = meanrat, colour = factor(id1))) +
ylim(-100, 100) +
labs(title=TITLE) +
ylab("Mean Rating (%)") +
xlab(measure)
print(p)
dev.off()
我知道图像的质量非常差,但它表明它是正确的。然而,从用于构建函数的模型中获得的系数看起来与该图不同:
summary(moddist3b)
Call:
lm(formula = meanrat ~ poly(range, degree), data = distcur)
Residuals:
9 10 11 12 13 14 15 16
-0.20134 0.44939 1.65996 -2.80500 -1.14594 2.98617 -0.92081 -0.02244
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.6770 0.8281 2.025 0.1128
poly(range, degree)1 -37.7155 2.3423 -16.102 8.7e-05 ***
poly(range, degree)2 -2.9435 2.3423 -1.257 0.2773
poly(range, degree)3 6.4888 2.3423 2.770 0.0503 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 2.342 on 4 degrees of freedom
Multiple R-squared: 0.9853, Adjusted R-squared: 0.9743
F-statistic: 89.51 on 3 and 4 DF, p-value: 0.0004019
给予函数y = 6.49x ^ 3 -2.94x ^ 2 - 37.72x + 1.68
在谷歌上清楚显示该功能与R(来自模型)的情节完全不同
答案 0 :(得分:6)
您遇到的问题与ggplot
无关。相反,它是您定义线性模型的方式。顺便说一句,我弄清楚发生了什么的方法是预测0:
R> (moddist3b <- lm(meanrat ~ poly(range, 3), distcur) )
Coefficients:
(Intercept) poly(range, 3)1 poly(range, 3)2 poly(range, 3)3
1.68 -37.72 -2.94 6.49
R> predict(moddist3b, data.frame(range = 0))
1
2.733
并注意到预测已关闭(应为1.68)。
无论如何,你需要使用参数raw=TRUE
(moddist3b <- lm(meanrat ~ poly(range, 3, raw=TRUE), distcur) )
predict(moddist3b, data.frame(range = 0))
这可以满足您的期望。默认情况下,poly
适用于正交多项式。有关详细信息,请参阅this blog post和poly帮助页。