R语言-相关性检验及线性拟合
2020-06-02 本文已影响0人
科研小徐
相关性检验
相关性检验R=1时为完全正相关。R=-1为完全负相关。R=0为正态分布
![](https://img.haomeiwen.com/i23603909/97b41d6f6a037b3d.png)
斜率与R值无关
相关性检验cor.test(变量1,变量2)
test=read.table("BMI.txt",sep="\t",header = T,row.names = 1)
test
plot(test$weight,test$height)
#相关性R值
cor(test$weight,test$height)
#相关性检验输出P值
cor.test(test$weight,test$height)
![](https://img.haomeiwen.com/i23603909/db6fb386492f4982.png)
输出P值为0.0122显示明显正相关
线性拟合
计算直线:
lm(纵坐标,横坐标,data=数据框)
图加直线:
abline(直线数据,col=”颜色“,lwd=数值)
lwd为线的宽度
result=lm(height~weight,data=test)
result
summary(result)
plot(test$weight,test$height)
#绘制的图中加直线拟合
abline(result,col="red",lwd=2)
![](https://img.haomeiwen.com/i23603909/bbd39ea044487267.png)