我运行了一个 ANCOVA 模型,以测试处理对两个连续变量(大象数量和植物密度)之间关系的影响 - 请在我的最后一个问题中查看更多详细信息(什么统计模型可以包含我的分类变量(2 个级别)和 2连续变量?)但是当在散点图上可视化我的结果时,有些东西不太正确。这是我的 ANCOVA 总结结果:
lm(formula = eledens ~ treat * plants, data = elemice)
Residuals:
Min 1Q Median 3Q Max
-7.102 -2.715 0.264 1.814 9.235
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 8.3028 1.8952 4.381 0.000097820 ***
treatMice added -0.7066 2.7105 -0.261 0.795810
plants 0.7368 0.1232 5.978 0.000000743 ***
treatMice added:plants -0.6840 0.1613 -4.241 0.000148 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.856 on 36 degrees of freedom
Multiple R-squared: 0.7393, Adjusted R-squared: 0.7176
F-statistic: 34.04 on 3 and 36 DF, p-value: 0.0000000001308
这是我试图可视化结果的绘图代码:
plot(eledens~plants, data=elemice, type="n", xlab="Plant Density", ylab="Elephant Density")
points(elemice$plants[elemice$treat=="Control"], elemice$eledens[elemice$treat=="Control"], col="skyblue3", pch=16)
points(elemice$plants[elemice$treat=="Mice added"], elemice$eledens[elemice$treat=="Mice added"], col="salmon", pch=16)
abline(fit.mice$coefficients[1:2], col="skyblue3")
abline(fit.mice$coefficients[1]+fit.mice$coefficients[3],fit.mice$coefficients[2], col="salmon")
new.x <- rep(seq(min(elemice$plants), max(elemice$plants), len=100),2)
new.s <- rep(c("Control","Mice added"), each=100)
pred <- predict(fit.mice, new=data.frame(plants=new.x, treat=new.s), interval="conf")
pred <- data.frame(pred, treat=new.s, plants=new.x)
head(pred)
lines(new.x[1:100],pred[1:100,"lwr"],lty=2, col="skyblue3")
lines(new.x[1:100],pred[1:100,"upr"],lty=2, col="skyblue3")
lines(new.x[101:200],pred[101:200,"lwr"],lty=2, col="salmon")
lines(new.x[101:200],pred[101:200,"upr"],lty=2, col="salmon")
legend("topleft", pch=16, col=c("skyblue3","salmon"), legend=c("Control","Mice added"))
正如您在下面看到的(参见输出图),系数的拟合线看起来有点奇怪,这是否意味着我的模型不正确?还是我在编码中错误地分配了系数?我对 R 编码相当陌生,因此非常感谢有关解释这一点的建议。