我正在尝试将某些模型拟合到某些数据中,并且生成的模型可以预测合理的值,并且这些图看起来是正确的。但是当提取系数并分别绘制函数时,它们没有任何意义!我显然做错了什么,所以请有人告诉我错误在哪里?
数据:
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 中的图(来自模型)完全不同