### 데이타 cars를 이용
### lm() 결과물
> summary(lm(dist~speed, data=cars))
Call:
lm(formula = dist ~ speed, data = cars)
Residuals:
Min 1Q Median 3Q Max
-29.069 -9.525 -2.272 9.215 43.201
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -17.5791 6.7584 -2.601 0.0123 *
speed 3.9324 0.4155 9.464 1.49e-12 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 15.38 on 48 degrees of freedom
Multiple R-squared: 0.6511, Adjusted R-squared: 0.6438
F-statistic: 89.57 on 1 and 48 DF, p-value: 1.490e-12
### R로 직접 계산하기
Y<-cars$dist
X<-cbind(rep(1,length(Y)),cars$speed)
df<-length(Y) - ncol(X)
# estimate
beta<- solve(t(X)%*%X)%*%t(X)%*%Y
> beta
[,1]
[1,] -17.579095
[2,] 3.932409
# R-square
residual<- Y - X%*%beta
SSE<- t(residual)%*%residual
SST<- t(Y-mean(Y))%*%(Y-mean(Y))
R.square <- 1 -SSE/SST
> R.square
[,1]
[1,] 0.6510794
# adjusted R-square: 각각의 df로 나눈후 비율
R.square.adj<-1 - (SSE/df)/(SST/(length(Y)-1))
> R.square.adj
[,1]
[1,] 0.6438102
# F
F.stat<- (SST-SSE)/(SSE/df)
> F.stat
[,1]
[1,] 89.5671
# sigma2: residual variance
sigma2<- SSE / df
> sqrt(sigma2) # residual std error
[,1]
[1,] 15.37959
# Std. Error of beta
sd.beta<-sqrt(diag(solve(t(X)%*%X))*sigma2)
> sd.beta
[1] 6.7584402 0.4155128
# t value of beta
t.beta<-beta/sd.beta
> t.beta
[,1]
[1,] -2.601058
[2,] 9.463990
# p.value of beta
p.value<-2*(1-pt(abs(t.beta),df=df))
> p.value
[,1]
[1,] 1.231882e-02
[2,] 1.489919e-12