Example 1

rm(list = ls())

x=c(0.3, 1.4,1.0,-0.3,-0.2,1.0,2.0,-1.0,-0.7,0.7)
y=c(0.4,0.9,0.4,-0.3,0.3,0.8,0.7,-0.4,-0.2,0.7)

plot(x,y, xlab="x-axis", ylab="y-axis")
title(main="First Example")

out=lm(y~x)
summary(out)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.31763 -0.16482  0.03983  0.19906  0.24814 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.14721    0.08257   1.783 0.112463    
## x            0.43521    0.08192   5.313 0.000717 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2374 on 8 degrees of freedom
## Multiple R-squared:  0.7792, Adjusted R-squared:  0.7515 
## F-statistic: 28.22 on 1 and 8 DF,  p-value: 0.0007174
5.313^2
## [1] 28.22797
anova(out)
## Analysis of Variance Table
## 
## Response: y
##           Df  Sum Sq Mean Sq F value    Pr(>F)    
## x          1 1.59025 1.59025  28.224 0.0007174 ***
## Residuals  8 0.45075 0.05634                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#1a
names(out)
##  [1] "coefficients"  "residuals"     "effects"       "rank"         
##  [5] "fitted.values" "assign"        "qr"            "df.residual"  
##  [9] "xlevels"       "call"          "terms"         "model"
names(summary(out))
##  [1] "call"          "terms"         "residuals"     "coefficients" 
##  [5] "aliased"       "sigma"         "df"            "r.squared"    
##  [9] "adj.r.squared" "fstatistic"    "cov.unscaled"
out$coefficients
## (Intercept)           x 
##   0.1472130   0.4352072
b0hat=out$coefficients[1]
summary(out)$coefficients
##              Estimate Std. Error  t value     Pr(>|t|)
## (Intercept) 0.1472130 0.08257239 1.782835 0.1124628771
## x           0.4352072 0.08191963 5.312612 0.0007173926
b0stderr=summary(out)$coefficients[1,2]
t=(b0hat-0.7)/b0stderr
t
## (Intercept) 
##   -6.694575
qt(0.975,8)
## [1] 2.306004
#1d
b1hat=out$coefficients[2]
theta.hat=5*b0hat-b1hat
theta.hat
## (Intercept) 
##   0.3008576
a1=sum((5*x+1)^2)
a1
## [1] 306
a2=10*9*var(x)
a2
## [1] 83.96
a=a1/a2
 
s=summary(out)$sigma
t.theta=theta.hat/(sqrt(a)*s)
t.theta
## (Intercept) 
##   0.6639146
qt(0.975,8)
## [1] 2.306004

Example 2

rm(list = ls())

x=c(71,64,43,67,56,73,68,56,76,65,45,58,45,53,49,78)
y=c(82,91,100,68,87,73,78,80,65,84,116,76,97,100,105,77)

plot(x,y, xlab="x-axis", ylab="y-axis")
title(main="Second Example")

fit=lm(y~x)

summary(fit)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -12.6825  -5.0563  -0.3876   6.7444  14.0108 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 148.0507    11.5629  12.804 4.05e-09 ***
## x            -1.0236     0.1882  -5.439 8.72e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 8.344 on 14 degrees of freedom
## Multiple R-squared:  0.6788, Adjusted R-squared:  0.6559 
## F-statistic: 29.59 on 1 and 14 DF,  p-value: 8.721e-05
fit$coef
## (Intercept)           x 
##  148.050676   -1.023589
lines(x,fit$fitted.values, col=2)

-qt(0.95,14)
## [1] -1.76131
test=data.frame(x=60) 
predict(fit, test, interval="conf") 
##        fit      lwr     upr
## 1 86.63532 82.15794 91.1127
predict(fit, test, interval="pred")
##        fit      lwr      upr
## 1 86.63532 68.18813 105.0825
predict(fit,test, interval="conf", level=0.95)
##        fit      lwr     upr
## 1 86.63532 82.15794 91.1127
predict(fit,test, interval="conf", level=0.99)
##        fit      lwr      upr
## 1 86.63532 80.42097 92.84967