#计算回归曲线,并预测x = 1300时的值
A = matrix(nrow = 10,ncol = 2)
A[,1] = c(5800,4980,6200,6800,7100,8900,9000,8100,9990,9800)
A[,2] = c(560,530,600,680,750,830,880,830,980,1100)
B = A[,2]-mean(A[,2])
C = A[,1]-mean(A[,1])
A3 = B*B
A4 = B*C
A5 = C*C
A = cbind(A,A3,A4,A5)
b = sum(A[,4])/sum(A[,3])
a = mean(A[,1])-b*mean(A[,2])
A = cbind(A,a+b*A[,2])
colnames(A) = c("y","x","(x-mean(x))^2","(x-mean(x))(y-mean(y))","(y-mean(y))^2","a+b*y")
rownames(A) = c(1:10)
w = b*1300+a
w
#用r自带函数验证验证
r = lm(A[,1]~A[,2])
a;b;r #对比验证
#画图(散点图加上回归直线)
plot(A[,2],A[,1])
abline(r)
学习R语言编程——计算回归直线
最新推荐文章于 2024-05-28 10:03:47 发布