我正在尝试将回归线方程,R square和P值添加到我的情节中,对我如何做到的任何想法?
以下是我在R中使用的代码:
plot<- plot(Q,CALCIUM.DISSOLVED)
fit<-(lm(CALCIUM.DISSOLVED ~ Q))
abline (fit)
答案 0 :(得分:1)
使用数据集汽车
m <- lm(dist ~ speed, data = cars)
plot(dist ~ speed,data = cars)
abline(m, col = "red")
这里我们使用summary()来提取R平方和P值。
m1 <- summary(m)
mtext(paste0("R squared: ",round(m1$r.squared,2)),adj = 0)
mtext(paste0("P-value: ", format.pval(pf(m1$fstatistic[1], # F-statistic
m1$fstatistic[2], # df
m1$fstatistic[3], # df
lower.tail = FALSE))))
mtext(paste0("dist ~ ",round(m1$coefficients[1],2)," + ",
round(m1$coefficients[2],2),"x"),
adj = 1)