diff options
Diffstat (limited to 'weightedLeastSquares.R')
-rw-r--r-- | weightedLeastSquares.R | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/weightedLeastSquares.R b/weightedLeastSquares.R new file mode 100644 index 0000000..3cad7a8 --- /dev/null +++ b/weightedLeastSquares.R @@ -0,0 +1,13 @@ +# Purpose: to show weighted least squares +# http://stat.cmu.edu/~cshalizi/350/lectures/18/lecture-18.pdf +n = 100 +x = rnorm(n,0,3) +y = 3-2*x + rnorm(n,0,sapply(x,function(x){1+0.5*x^2})) +plot(x,y) +abline(a=3,b=-2,col='red') +fit.ols = lm(y~x) +abline(fit.ols$coefficients,lty=1, col='grey') +fit.wls = lm(y~x, weights=1/(1+0.5*x^2)) +abline(fit.wls$coefficients,lty=1, col='black') +summary(fit.ols) +summary(fit.wls)
\ No newline at end of file |