多重線形回帰シミュレーション


14

R言語は初めてです。回帰の4つの仮定すべてを満たす多重線形回帰モデルからシミュレーションする方法を知りたい。


わかりました。ありがとう。

このデータセットに基づいてデータをシミュレートしたいとしましょう:

y<-c(18.73,14.52,17.43,14.54,13.44,24.39,13.34,22.71,12.68,19.32,30.16,27.09,25.40,26.05,33.49,35.62,26.07,36.78,34.95,43.67)
x1<-c(610,950,720,840,980,530,680,540,890,730,670,770,880,1000,760,590,910,650,810,500)
x2<-c(1,1,3,2,1,1,3,3,2,2,1,3,3,2,2,2,3,3,1,2)

fit<-lm(y~x1+x2)
summary(fit)

次に、出力を取得します:

Call:
lm(formula = y ~ x1 + x2)

Residuals:
     Min       1Q   Median       3Q      Max 
-13.2805  -7.5169  -0.9231   7.2556  12.8209 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)   
(Intercept) 42.85352   11.33229   3.782  0.00149 **
x1          -0.02534    0.01293  -1.960  0.06662 . 
x2           0.33188    2.41657   0.137  0.89238   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1   1

Residual standard error: 8.679 on 17 degrees of freedom
Multiple R-squared:  0.1869,    Adjusted R-squared:  0.09127 
F-statistic: 1.954 on 2 and 17 DF,  p-value: 0.1722

私の質問は、上記の元のデータを模倣する新しいデータをシミュレートする方法ですか?

回答:


28
  1. まだ持っていない場合は、いくつかの予測変数x 2、...を設定することから始めます。x1x2

  2. あなたの予測変数の係数(「真」)の人口を選択して、さん、を含むβ 0、インターセプト。βiβ0

  3. σ2σ

  4. εσ2

  5. y=β0+β1x1+β2x2+...+βkxk+ε

yx

たとえば、Rでは次のようなことができます。

x1 <- 11:30
x2 <- runif(20,5,95)
x3 <- rbinom(20,1,.5)

b0 <- 17
b1 <- 0.5
b2 <- 0.037
b3 <- -5.2
sigma <- 1.4

eps <- rnorm(x1,0,sigma)
y <- b0 + b1*x1  + b2*x2  + b3*x3 + eps

y

 summary(lm(y~x1+x2+x3))

与える

Call:
lm(formula = y ~ x1 + x2 + x3)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.6967 -0.4970  0.1152  0.7536  1.6511 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 16.28141    1.32102  12.325 1.40e-09 ***
x1           0.55939    0.04850  11.533 3.65e-09 ***
x2           0.01715    0.01578   1.087    0.293    
x3          -4.91783    0.66547  -7.390 1.53e-06 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1   1

Residual standard error: 1.241 on 16 degrees of freedom
Multiple R-squared:  0.9343,    Adjusted R-squared:  0.9219 
F-statistic: 75.79 on 3 and 16 DF,  p-value: 1.131e-09

この手順はいくつかの方法で簡素化できますが、最初からスペルを入力すると役立つと考えました。

yepsy


推定値の標準誤差を変更することは可能ですか?少し変更したスクリプト(rnorm()ではなく11:30)を使用しましたが、エラー(シグマ)をどれだけ増やしても、推定の標準エラーはほぼ同じです。
ダニエル

2

正規分布に従うエラーを伴う多重線形回帰を生成する別のコードを次に示します。

sim.regression<-function(n.obs=10,coefficients=runif(10,-5,5),s.deviation=.1){

  n.var=length(coefficients)  
  M=matrix(0,ncol=n.var,nrow=n.obs)

  beta=as.matrix(coefficients)

  for (i in 1:n.var){
    M[,i]=rnorm(n.obs,0,1)
  }

  y=M %*% beta + rnorm(n.obs,0,s.deviation)

  return (list(x=M,y=y,coeff=coefficients))

}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.