1. simple linear regression
x.vec = c(4,8,9,8,8,12,6,10,6,9) 
y.vec = c(9,20,22,15,17,30,18,25,10,20) 
  
fit = lm(y.vec~x.vec) 
summary(fit) 
## 
## Call:
## lm(formula = y.vec ~ x.vec)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.600 -1.502  0.813  1.128  4.617 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -2.2696     3.2123  -0.707 0.499926    
## x.vec         2.6087     0.3878   6.726 0.000149 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.631 on 8 degrees of freedom
## Multiple R-squared:  0.8497, Adjusted R-squared:  0.831 
## F-statistic: 45.24 on 1 and 8 DF,  p-value: 0.0001487
simple.fun = function(x,y){ 
b1 = sum((x-mean(x))*(y-mean(y)))/sum((x-mean(x))^2) 
b0 = mean(y)-b1*mean(x) 
return(c(b1.hat=b1,b0.hat=b0)) 
} 
simple.fun(x.vec,y.vec)
##    b1.hat    b0.hat 
##  2.608696 -2.269565
  1. ANOVA
x.vec = c(4,8,9,8,8,12,6,10,6,9) 
y.vec = c(9,20,22,15,17,30,18,25,10,20) 

fit = lm(y.vec~x.vec) 
fit.anova = anova(fit)
n=10 
simple.fun = function(x,y){ 
 syy = sum((y-mean(y))^2); sxy = sum((x-mean(x))*(y-mean(y))) 
 sxx = sum((x-mean(x))^2) 
 sst = syy; ssr = sxy^2/sxx; sse = sst-ssr 
 msr = ssr; mse = sse/(n-2) 
 f0 = msr/mse 
 return(c(F0=f0)) 
} 
simple.fun(x.vec,y.vec) 
##       F0 
## 45.24034
  1. weignted linear regression
x.vec = c(4,8,9,8,8,12,6,10,6,9) 
y.vec = c(9,20,22,15,17,30,18,25,10,20)
sd = c(0.3,0.2,0.2,-0.5,2.0,4.5,0.4,4.4,-0.1,0.5) 
wi = 1/sd^2 
fit = lm(y.vec~x.vec) 
summary(fit) 
## 
## Call:
## lm(formula = y.vec ~ x.vec)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.600 -1.502  0.813  1.128  4.617 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -2.2696     3.2123  -0.707 0.499926    
## x.vec         2.6087     0.3878   6.726 0.000149 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.631 on 8 degrees of freedom
## Multiple R-squared:  0.8497, Adjusted R-squared:  0.831 
## F-statistic: 45.24 on 1 and 8 DF,  p-value: 0.0001487
fit.weight = lm(y.vec~x.vec,weights=wi) 
summary(fit.weight) 
## 
## Call:
## lm(formula = y.vec ~ x.vec, weights = wi)
## 
## Weighted Residuals:
##     Min      1Q  Median      3Q     Max 
## -13.460  -2.272  -0.143   7.971  16.635 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -8.8137     3.7672  -2.340 0.047447 *  
## x.vec         3.3599     0.5503   6.106 0.000288 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 10.11 on 8 degrees of freedom
## Multiple R-squared:  0.8233, Adjusted R-squared:  0.8013 
## F-statistic: 37.29 on 1 and 8 DF,  p-value: 0.0002875
plot(y.vec~x.vec,cex=1.3) 
lines(x.vec,predict(fit),col="blue",lwd=2) 
lines(x.vec,predict(fit.weight),col="red",lwd=2) 
legend("topleft",c("weighted","unweighted"),lty=c(1,1),lwd=c(2,2),
         col=c("red","blue"),bty="n")