简单线性回归分析

2019-02-18  本文已影响2人  Natsuka

使用包basicTrendline。

安装包

install.packages("basicTrendline")

查看已经安装的包

installed.packages("basicTrendline")

调用包

library("basicTrendline")

载入数据

mydata<-read.table("C:/Users/Administrator/Desktop/11.csv",sep=",")

为了方便调用,使用attach方法

attach(mydata)

线性回归

lm.model<-lm(V2~V1+1) #有截距的形式
summary(lm.model) #查看模型
lm.model<-lm(V2~V1-1) #没有截距的形式,即y=ax+b中,b=0
summary(lm.model) #查看模型

结果

Call:
lm(formula = V2 ~ V1 - 1)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.7126 -0.6409 -0.2093  0.4317  4.4799 

Coefficients:
    Estimate Std. Error t value Pr(>|t|)    
V1 0.1710027  0.0001303    1312   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.8798 on 1199 degrees of freedom
Multiple R-squared:  0.9993,    Adjusted R-squared:  0.9993 
F-statistic: 1.722e+06 on 1 and 1199 DF,  p-value: < 2.2e-16

使用basicTrendline包的回归拟合

trendline(x, y, model="line2P", ePos.x = "topleft", summary=TRUE, eDigit=5) #自动添加95%置信区间lines and fill color
image.png
trendline(V1, V2, model="line3P", CI.fill = FALSE, CI.color = "black", CI.lty = 2, linecolor = "blue") #只添加95%置信区间的lines,不fill color (set CI.fill = FALSE)
image.png
trendline(V1, V2, model="log2P", ePos.x= "top", linecolor = "red", CI.color = NA) #只绘制回归曲线,不添加95%置信区间 (set CI.color = NA)
image.png
trendline(V1, V2, model="line3P", show.equation = TRUE, show.Rpvalue = FALSE)  #显示方程,不显示R值和P值 (set show.Rpvalue = FALSE)
image.png
trendline(V1, V2, model="line3P", xname="a", yname=paste(beta^15,b), yhat=FALSE, Rname=1, Pname=0, ePos.x = "bottom") #自定义方程中的参数的名称:‘xname’, ‘yname’, ‘yhat’, ‘Rname’, ‘Pname’
image.png
trendline(V1, V2, model="power2P", ePos.x = "topleft", summary=TRUE, eDigit = 3, eSize = 1.4, text.col = "blue") #改变方程的 小数位,字体颜色,字号大小
image.png
trendline(V1, V2, model="power2P",ePos.x = NA) #不显示方程,只显示回归曲线 (set ePos.x = NA)
image.png
#设置绘图区大小
par(mgp=c(1.5,0.4,0), mar=c(3,3,1,1), tck=-0.01, cex.axis=0.9)
trendline(V1, V2)
image.png
上一篇下一篇

猜你喜欢

热点阅读