function for weighted least squares estimates

◇◆丶佛笑我妖孽 提交于 2019-12-20 10:46:32

问题


Does R have a function for weighted least squares? Specifically, I am looking for something that computes intercept and slope.

Data sets

  1. 1 3 5 7 9 11 14 17 19 25 29
  2. 17 31 19 27 31 62 58 35 29 21 18
  3. 102153 104123 96564 125565 132255 115454 114555 132255 129564 126455 124578

The dependent variable is dataset 3 and dataset 1 and 2 are the independent variables.


回答1:


Yes, of course, there is a weights= option to lm(), the basic linear model fitting function. Quick example:

R> df <- data.frame(x=1:10)
R> lm(x ~ 1, data=df)            ## i.e. the same as mean(df$x)

Call:
lm(formula = x ~ 1, data = df)

Coefficients:
(Intercept)  
        5.5  

R> lm(x ~ 1, data=df, weights=seq(0.1, 1.0, by=0.1))

Call:
lm(formula = x ~ 1, data = df, weights = seq(0.1, 1, by = 0.1))

Coefficients:
(Intercept)  
          7  

R> 

so by weighing later observations more heavily the mean of the sequence 1 to 10 moves from 5.5 to 7.




回答2:


First, create your datasets. I'm putting them into a single data.frame but this is not strictly necessary.

dat <- data.frame(x1 = c(1,3,5,7,9,11,14,17,19,25, 29)
                  , x2 = c(17, 31, 19, 27, 31, 62, 58, 35, 29, 21, 18)
                  , y  = c(102153, 104123, 96564, 125565, 132255, 115454
                           , 114555, 132255, 129564, 126455, 124578)
                  )

Second, estimate the model:

> lm(y ~ x1 + x2, data = dat)

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

Coefficients:
(Intercept)           x1           x2  
  104246.37       906.91        85.76

Third, add your weights as necessary following @Dirk's suggestions.

Fourth and most importantly - read through a tutorial or two on regression in R. Google turns this up as a top hit: http://www.jeremymiles.co.uk/regressionbook/extras/appendix2/R/




回答3:


Just another take on this. You can create a weight matrix first. For example:

samplevar = var(ydata)

M = diag(40,1/samplevar)

At this point M is a 40x40 diagonal matrix. You can convert to a vector by applying diag to M:

M_vector = diag(M)

Then use this in lm :

   lm ( YXDATAFRAME, weights=M_vector)


来源:https://stackoverflow.com/questions/6375650/function-for-weighted-least-squares-estimates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!