学习R语言编程——计算回归直线

#计算回归曲线,并预测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)
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值