From 6eb711beb4a789d262fcd791b6a0bb1f19064fc1 Mon Sep 17 00:00:00 2001 From: Hui Lan Date: Thu, 11 Apr 2019 09:26:58 +0800 Subject: Initial commit Lecture Notes on Artifical Intelligence --- weightedLeastSquares.R | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 weightedLeastSquares.R (limited to 'weightedLeastSquares.R') 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 -- cgit v1.2.1