Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Qusay abusaymeh

hw5
> y<-matrix(c(25,31,27,28,36,35,32,34),ncol = 1)
>y
[,1]
[1,] 25
[2,] 31
[3,] 27
[4,] 28
[5,] 36
[6,] 35
[7,] 32
[8,] 34
> x<-matrix(c(1,1,1,1,1,1,1,1,1,1.5,2,2.5,3,3.5,4,4.5),ncol = 2, nrow = 8)
>x
[,1] [,2]
[1,] 1 1.0
[2,] 1 1.5
[3,] 1 2.0
[4,] 1 2.5
[5,] 1 3.0
[6,] 1 3.5
[7,] 1 4.0
[8,] 1 4.5
> yty<-t(y)%*%y
> yty
[,1]
[1,] 7800
> xtx<-t(x)%*%x
> xtx
[,1] [,2]
[1,] 8 22
[2,] 22 71
> xtx_inv<- solve(xtx)
> xtx_inv
[,1] [,2]
[1,] 0.8452381 -0.2619048
[2,] -0.2619048 0.0952381
> beta_vec<-xtx_inv%*% t(x)%*%y
> beta_vec
[,1]
[1,] 24.452381
[2,] 2.380952
> mse<-(yty-t(beta_vec)%*%t(x)%*%y)/(length(y)-2)
> mse
[,1]
[1,] 8.746032
> c_vec<- matrix(c(0,1) ,nrow = 1)
> c_vec
[,1] [,2]
[1,] 0 1
> beta1<-c_vec%*%beta_vec
> beta1
[,1]
[1,] 2.380952
> cc_vec<- matrix(c(1,0) ,nrow = 1)
> beta0<-cc_vec%*%beta_vec
> beta0
[,1]
[1,] 24.45238
> varb1<- c_vec%*%xtx_inv%*%t(c_vec)*as.numeric(mse)
> varb1
[,1]
[1,] 0.8329554
> t_stat<-beta1/sqrt(varb1)
> t_stat
[,1]
[1,] 2.608794
> pval1<-2*(1-pt(t_stat,df = length(y)-2))
> pval1
[,1]
[1,] 0.04018534
> lci<-beta1-qt(0.975,2)*sqrt(varb1)
> uci<-beta1+qt(0.975,2)*sqrt(varb1)
> c(lci,uci)
[1] -1.545923 6.307828
> modl<-lm(y~x)
> modl

Call:
lm(formula = y ~ x)

Coefficients:
(Intercept) x1 x2
24.452 NA 2.381

> summary(modl)

Call:
lm(formula = y ~ x)

Residuals:
Min 1Q Median 3Q Max
-2.405 -2.036 -1.500 2.405 4.405

Coefficients: (1 not defined because of singularities)


Estimate Std. Error t value Pr(>|t|)
(Intercept) 24.4524 2.7189 8.993 0.000106 ***
x1 NA NA NA NA
x2 2.3810 0.9127 2.609 0.040185 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.957 on 6 degrees of freedom


Multiple R-squared: 0.5315, Adjusted R-squared: 0.4534
F-statistic: 6.806 on 1 and 6 DF, p-value: 0.04019

You might also like