我試圖將某些模型擬合到某些數據,並且所得到的模型預測了合理的值並且曲線圖看起來是正確的。但是當提取係數並分別繪製函數時,它們是沒有意義的!我顯然做錯了,所以請有人告訴我錯誤在哪裏?R-多項式線性模型係數不適合模型的預測值
數據:
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的情節(從模型)
只是一個猜測,但是你可能想用'I(poly(range,degree))'把你的獨立參數括起來,這樣'公式'就可以按照你想要的方式來解釋。像「+」和「*」這樣的東西在R公式中有不同的含義。 –
@CarlWitthoft添加'I'給出了完全相同的模型,但是預測的值幾乎是一條水平線,這遠離實驗點。係數仍然與我的問題相同。不知道爲什麼它會影響預測,但我仍然沒有繪製線條的功能。 – unixsnob