線性函數擬合R語言樣本_r語言

來源:互聯網
上載者:User
線性函數擬合(y=a+bx)

1.       R運行執行個體

R語言運行代碼如下:綠色為要提供的資料,黃色標識資訊為需要儲存的。

 

x<-c(0.10,0.11, 0.12, 0.13, 0.14, 0.15,0.16, 0.17, 0.18, 0.20, 0.21, 0.23)

y<-c(42.0,43.5, 45.0, 45.5, 45.0, 47.5,49.0, 53.0, 50.0, 55.0, 55.0, 60.0)

data1=data.frame(x=x,y=y)  #資料存入資料框

 

#擬合線性函數

lm.data1<-lm(y~ x,data=data1)

summary(lm.data1)        #輸出擬合後資訊

Call:

lm(formula = data1$y ~ data1$x)

 

Residuals: #殘差分位元(當殘差個數較少時,此處顯示所有殘差值)

   Min      1Q   Median   3Q    Max

  -2.0431  -0.7056 0.1694  0.6633  2.2653

 

Coefficients:

       #係數估計值 #係數標準誤差 #t檢驗值 #對應t值機率的2倍

          Estimate     Std. Error      t value   Pr(>|t|)   

(Intercept)   28.493       1.580        18.04   5.88e-09 ***

data1$x    130.835      9.683        13.51   9.50e-08 ***

---

Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’1

 

Residual standard error: 1.319 on 10degrees of freedom

#擬合優度判定係數          #修正的擬合優度判定係數

Multiple R-squared:  0.9481,       AdjustedR-squared: 0.9429

#F檢驗值                   #F檢驗值對應的機率

F-statistic: 182.6 on 1 and 10 DF,  p-value:9.505e-08

t檢驗值對應的Pr(>|t|):該數值由於是t檢驗量對應的機率的2倍,所以其數值小於0.1時,就說明對應的自變數對於因變數來說是有顯著性的,當然數值越小,顯著性越高。

F檢驗值對應的p-value:F檢驗值對應的機率。當其數值小於0.05時,說明迴歸方程是顯著的。

Adjusted R-squared修正擬合優度:數值越接近1,模型擬合的越好。

/***********************數值輸出********************************/

coefficient<-lm.data1$coefficients  #迴歸所有係數列舉

coefficient[1]  #係數a:截距

coefficient [2]  #係數b

tvalue<-summary(lm.data1)$coefficients[1:2,3]         #t檢驗值

tvalue[1]                  #係數a對應的t檢驗值

tvalue[2]                  #係數b對應的t檢驗值

Pr<-summary(lm.data1)$coefficients[1:2,4]             #t檢驗值對應的機率的2倍

Pr[1]                     #係數a對應t檢驗值對應的機率2倍

Pr[2]                     #係數b對應t檢驗值對應的機率2倍

 

ARsquared<-summary(lm.data1)$adj.r.squared        #修正的擬合優度判定係數

 

F_info<-summary(lm.data1)$fstatistic               #F檢驗的F值、自由度DF和機率

F_info[1]                  #F檢驗值

F_info[2]                  #F檢驗量的自由度

F_info[3]                  #F檢驗值對應的機率

/****************************************************************/

 

#殘差正態性檢驗

ks.test(lm.data1$residuals,"pnorm",0)

      One-sampleKolmogorov-Smirnov test

 

data: lm.data1$residuals

#D檢驗值  #對應的機率值

D = 0.1332, p-value = 0.9648

alternative hypothesis: two-sided

D檢驗值對應的機率值p-value:該數值大於0.05,說明殘差服從均值為0的常態分佈。

/***********************數值輸出********************************/

ks_test<-ks.test(lm.data1$residuals,"pnorm",0)    #擷取正態性檢驗資訊

ks_test[1]               #KS檢驗值

ks_test[2]               #KS檢驗值對應的機率

/****************************************************************/

 

#殘差獨立性檢驗:DW法檢驗殘差序列的自相關性

dwtest(lm.data1)

      Durbin-Watsontest

 

data:  lm.data1

#d.w檢驗值#對應的機率值

DW = 2.5465, p-value = 0.7422

alternative hypothesis: trueautocorrelation is greater than 0

DW檢驗值對應的機率值p-value:該數值大於0.05時,說明殘差序列獨立。

/***********************數值輸出********************************/

dw_test<-dwtest(lm.data1)  #擷取獨立性檢驗資訊

dw_test[1]               #DW檢驗值

dw_test[4]               #DW檢驗值對應的機率

/****************************************************************/

 

#殘差同方差檢驗

bptest(lm.data1)

      studentizedBreusch-Pagan test

 

data: lm.data1

#BP檢驗值  #自由度 #對應機率值

BP = 0.9831,  df = 1,  p-value = 0.3214

BP檢驗值對應的機率值p-value:該數值大於0.05,說明殘差是同方差的。

/***********************數值輸出********************************/

bp_test<- bptest(lm.data1)  #擷取獨立性檢驗資訊

bp_test[1]               #BP檢驗值

bp_test[2]               #BP檢驗量的自由度

bp_test[4]               #BP檢驗值對應的機率

/****************************************************************/

2.       擬合曲線圖

ab<-round(lm.data1$coefficients[1],3)  #迴歸方程係數a,保留3位小數
bb<-round(lm.data1$coefficients[2],3)  #迴歸方程係數b,保留3位小數
plot(data1$x,data1$y,xlab="x",ylab = "y",col="red",pch="*") #訓練資料點

abline(lm.data1,col="blue")  #擬合曲線

text(mean(data1$x),max(data1$y),paste("y = ",bb,"x+(",ab,")",sep = ""))#方程式



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.