最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

R語言缺失值的處理:線性回歸模型插補(bǔ)

2021-04-21 12:54 作者:拓端tecdat  | 我要投稿

原文鏈接:?http://tecdat.cn/?p=14528

?

在當(dāng)我們?nèi)鄙僦禃r,系統(tǒng)會告訴我用-1代替,然后添加一個指示符,該變量等于-1。這樣就可以不刪除變量或觀測值。

視頻

缺失值的處理:線性回歸模型插補(bǔ)

我們在這里模擬數(shù)據(jù),然后根據(jù)模型生成數(shù)據(jù)。未定義將轉(zhuǎn)換為NA。一般建議是將缺失值替換為-1,然后擬合未定義的模型。默認(rèn)情況下,R的策略是刪除缺失值。如果未定義50%,則缺少數(shù)據(jù),將刪除一半的行

  1. n=1000

  2. x1=runif(n)

  3. x2=runif(n)

  4. e=rnorm(n,.2)

  5. y=1+2*x1-x2+e

  6. alpha=.05

  7. indice=sample(1:n,size=round(n*alpha))

  8. base=data.frame(y=y,x1=x1)

  9. base$x1[indice]=NA

  10. reg=lm(y~x1+x2,data=base)

?

我們模擬10,000,然后看看未定義的分布,

  1. m=10000

  2. B=rep(NA,m)








  3. hist(B,probability=TRUE,col=rgb(0,0,1,.4),border="white",xlab="missing values = 50%")

  4. lines(density(B),lwd=2,col="blue")

  5. abline(v=2,lty=2,col="red")

?

?

當(dāng)然,丟失值的比率較低-丟失的觀測值較少,因此估計(jì)量的方差較小。

?

現(xiàn)在讓我們嘗試以下策略:用固定的數(shù)值替換缺失的值,并添加一個指標(biāo),

  1. B=rep(NA,m)







  2. hist(B,probability=TRUE,col=rgb(0,0,1,.4),border="white")

  3. lines(density(B),lwd=2,col="blue")

  4. abline(v=2,lty=2,col="red")

?

?

不會有太大變化,遺漏值的比率下降到5%,

?

例如仍有5%的缺失值,我們有

?

如果我們查看樣本,尤其是未定義的點(diǎn),則會觀察到

?

缺失值是完全獨(dú)立地隨機(jī)選擇的,

  1. x1=runif(n)







  2. plot(x1,y,col=clr)

?

?

(此處缺失值的1/3為紅色)。但可以假設(shè)缺失值的最大值,例如,

  1. x1=runif(n)







  2. clr=rep("black",n)

  3. clr[indice]="red"

  4. plot(x1,y,col=clr)

?

?

有人可能想知道,估計(jì)量會給出什么?

?

它變化不大,但是如果仔細(xì)觀察,我們會有更多差異。如果未定義變量會發(fā)生什么,



  1. for(s in 1:m){











  2. base$x1[indice]=-1

  3. reg=lm(y~x1+x2+I(x1==(-1)),data=base)

  4. B[s]=coefficients(reg)[2]

  5. }


?

?

這次,我們有一個有偏差的估計(jì)量。

  1. set.seed(1)







  2. indice=sample(1:n,size=round(n*alpha),prob = x1^3)


  3. base$x1[indice]=-1





  4. coefficients(reg1)

  5. (Intercept) ? ? ? ? ? ? ? ?x1 ? ? ? ? ? ? ? ?x2 I(x1 == (-1))TRUE

  6. 1.0988005 ? ? ? ? 1.7454385 ? ? ? ?-0.5149477 ? ? ? ? 3.1000668

  7. base$x1[indice]=NA



  8. coefficients(reg2)

  9. (Intercept) ? ? ? ? ?x1 ? ? ? ? ?x2

  10. 1.1123953 ? 1.8612882 ?-0.6548206

?

正如我所說的,一種更好的方法是推算。這個想法是為未定義的缺失預(yù)測值預(yù)測。最簡單的方法是創(chuàng)建一個線性模型,并根據(jù)非缺失值進(jìn)行校準(zhǔn)。然后在此新基礎(chǔ)上估算模型。

  1. for(s in 1:m){







  2. base$x1[indice]=NA

  3. reg0=lm(x1~x2,data=base[-indice,])

  4. base$x1[indice]=predict(reg0,newdata=base[indice,])

  5. reg=lm(y~x1+x2,data=base)





  6. }

  7. hist(B,probability=TRUE,col=rgb(0,0,1,.4),border="white")

  8. lines(density(B),lwd=2,col="blue")

  9. abline(v=2,lty=2,col="red")

?

?

在數(shù)字示例中,我們得到

  1. base$x1[indice]=NA






  2. coefficients(reg3)

  3. (Intercept) ? ? ? ? ?x1 ? ? ? ? ?x2

  4. 1.1593298 ? 1.8612882 ?-0.6320339

?

這種方法至少能夠糾正偏差

然后,如果仔細(xì)觀察,我們獲得與第一種方法完全相同的值,該方法包括刪除缺少值的行。



  1. Coefficients:

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

  3. (Intercept) ?1.15933 ? ?0.06649 ?17.435 ?< 2e-16 ***

  4. x1 ? ? ? ? ? 1.86129 ? ?0.21967 ? 8.473 ?< 2e-16 ***

  5. x2 ? ? ? ? ?-0.63203 ? ?0.20148 ?-3.137 ?0.00176 **

  6. ---

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


  8. Residual standard error: 1.051 on 997 degrees of freedom

  9. Multiple R-squared: ?0.1094, Adjusted R-squared: ?0.1076

  10. F-statistic: 61.23 on 2 and 997 DF, ?p-value: < 2.2e-16




  11. Coefficients: Estimate Std. Error t value Pr(>|t|)

  12. (Intercept) ?1.11240 ? ?0.06878 ?16.173 ?< 2e-16 ***

  13. x1 ? ? ? ? ? 1.86129 ? ?0.21666 ? 8.591 ?< 2e-16 ***

  14. x2 ? ? ? ? ?-0.65482 ? ?0.20820 ?-3.145 ?0.00172 **

  15. ---

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


  17. Residual standard error: 1.037 on 797 degrees of freedom

  18. (200 observations deleted due to missingness)

  19. Multiple R-squared: ?0.1223, Adjusted R-squared: ? 0.12

  20. F-statistic: ?55.5 on 2 and 797 DF, ?p-value: < 2.2e-16

?

除了進(jìn)行線性回歸外,還可以使用另一種插補(bǔ)方法。

在模擬的基礎(chǔ)上,我們獲得



  1. for(j in indice) base0$x1[j]=kpp(j,base0,k=5)

  2. reg4=lm(y~x1+x2,data=base)

  3. coefficients(reg4)

  4. (Intercept) ? ? ? ? ?x1 ? ? ? ? ?x2

  5. 1.197944 ? ?1.804220 ? -0.806766

?

如果我們看一下10,000個模擬中的樣子,就會發(fā)現(xiàn)

  1. for(s in 1:m){








  2. base0=base

  3. for(j in indice) base0$x1[j]=kpp(j,base0,k=5)

  4. reg=lm(y~x1+x2,data=base0)

  5. B[s]=coefficients(reg)[2]

  6. }

  7. hist(B,probability=TRUE,col=rgb(0,0,1,.4),border="white")

  8. lines(density(B),lwd=2,col="blue")

  9. abline(v=2,lty=2,col="red")

?

?

這里的偏差似乎比沒有插補(bǔ)時要弱一些,換句話說,在我看來,插補(bǔ)方法似乎比旨在用任意值替換NA并在回歸中添加指標(biāo)的策略更強(qiáng)大。

?

參考文獻(xiàn)

1.用SPSS估計(jì)HLM層次線性模型模型

2.R語言線性判別分析(LDA),二次判別分析(QDA)和正則判別分析(RDA)

3.基于R語言的lmer混合線性回歸模型

4.R語言Gibbs抽樣的貝葉斯簡單線性回歸仿真分析

5.在r語言中使用GAM(廣義相加模型)進(jìn)行電力負(fù)荷時間序列分析

6.使用SAS,Stata,HLM,R,SPSS和Mplus的分層線性模型HLM

7.R語言中的嶺回歸、套索回歸、主成分回歸:線性模型選擇和正則化

8.R語言用線性回歸模型預(yù)測空氣質(zhì)量臭氧數(shù)據(jù)

9.R語言分層線性模型案例


R語言缺失值的處理:線性回歸模型插補(bǔ)的評論 (共 條)

分享到微博請遵守國家法律
波密县| 红桥区| 凤城市| 永丰县| 宽甸| 贡觉县| 石楼县| 五河县| 沂源县| 封丘县| 健康| 沂源县| 荥经县| 布尔津县| 济宁市| 成武县| 惠来县| 临颍县| 平顺县| 凤台县| 高雄市| 华阴市| 丰镇市| 塘沽区| 汾西县| 屏山县| 安仁县| 安丘市| 隆安县| 饶平县| 闽清县| 安康市| 朝阳区| 嵊泗县| 抚松县| 渭南市| 衡南县| 芜湖县| 嵩明县| 岳普湖县| 曲麻莱县|