Confirming SSR, SSE, and SST using matrix in R
[This article was first published on ALSTAT R Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The codes below was done in our regression laboratory class. Here, we run first the data in SPSS, and take the ANOVA output where we can find the computed values of SSR, SSE, and SST.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Al-Ahmadgaid Asaad | |
#As part of the yearly report of the manager of human | |
#resources at St. Luke’s Medical Center, he is requested to | |
#present analysis of the salaried employees. He selects a | |
#random sample of 30, from over 1,000 employees, and records | |
#the Monthly salary, service in months, and age of the | |
#employee. | |
table #The table here was not generated from R but from SPSS |
ANOVAb | ||||||
Model | Sum of Squares | df | Mean Square | F | Sig. | |
1 | Regression | 640267.552 | 1 | 640267.552 | 9.822 | .004a |
Residual | 1825213.815 | 28 | 65186.208 | |||
Total | 2465481.367 | 29 | ||||
a. Predictors: (Constant), Length of Service | ||||||
b. Dependent Variable: Monthly Salary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Solving for the values of SSR,SSE, and SST using matrix in R. | |
#Codes: | |
#Lets input first everything we need in the computations. | |
#Inputting the given for the dependent and independent value. | |
IndependentVar <-c(1,93,1,104,1,104,1,126,1,98,1,99,1,94,1,96, | |
[17] 1,124,1,73,1,110,1,90,1,104,1,81,1,106,1,113,1,129,1,97,1, | |
[37] 101,1,91,1,100,1,123,1,88,1,117,1,107,1,105,1,86,1,131,1, | |
[58] 95,1,98) | |
DependentVar <- c(1769,1740,1941,2367,2467,1640,1756,1706, | |
[9] 1767,1200,1706,1985,1555,1749,2056,1729,2186,1858,1819, | |
[20] 1350,2030,2550,1544,1766, 1937,1691,1623,1791,2001,1874) | |
#Next we put them in a matrix | |
X <- matrix(IndependentVar, ncol = 2, byrow = T) | |
Y <- matrix(DependentVar, ncol = 1, byrow = T) | |
#This time lets solve for the X’ and Y’. | |
XPrime <- t(X) #transposing X | |
YPrime <- t(Y) #transposing Y | |
#Now, lets make an Identity matrix | |
n <- c(30) #here we assign our n equal to 30 | |
I <- matrix(0, nrow = n, ncol = n) #we define an n by n | |
#matrix here that has an entries of 0. | |
I[row(I) == col(I)] <- 1 #here we assign a value 1 if the ith | |
#rows is equal to the jth columns. | |
#Since we have our Identity matrix now, it’s easy for us then | |
#to make a J matrix that consist of entries 1. | |
J <- matrix(1, nrow = n, ncol = n) | |
#We are almost ready for computation, but before that we need | |
#to define first our H. To do that lets define the inverse | |
#first. | |
XXPrime <- XPrime%*%X #here we define X’X | |
Inverse <- solve(XXPrime) #here we have (X’X)^(-1) | |
H <- X%*%Inverse%*%XPrime | |
#Ok we’re done with all the variables we need in our | |
#computation, this time lets compute for SSR. | |
SSRCen <- H – J/30 #here we define (H – J/n) | |
SSR <- YPrime%*%SSRCen%*%Y | |
SSR | |
[,1] | |
[1,] 640267.6 | |
#Next, we compute for SSE | |
SSECen <- I – H #here we define (I – H) | |
SSE <- YPrime%*%SSECen%*%Y | |
SSR | |
[,1] | |
[1,] 1825214 | |
#Time to smile, we are approaching to the finish line. Lets | |
#make it fast and solve for SST :) | |
SSTCen <- I – J/30 #here we define (I – J/n) | |
SST <- YPrime%*%SSTCen%*%Y | |
SST | |
[,1] | |
[1,] 2465481 | |
#Ok there you go, we got the values of SSR, SSE, and SST. You | |
#can confirm that in SPSS (we did it actually). | |
#Moreover, you can also confirm SSR by using SSR = SST – SSE | |
SSR <- SST – SSE | |
SSR | |
[,1] | |
[1,] 640267.6 | |
#In addition, we can also solve for matrix beta’s, i.e. the | |
#beta null and beta one. | |
Beta <- Inverse%*%XPrime%*%Y | |
Beta | |
[,1] | |
[1,] 761.04720 | |
[2,] 10.48381 | |
#There you go, everything is done, and we’re ready to submit | |
#it. :) |
To leave a comment for the author, please follow the link and comment on their blog: ALSTAT R Blog.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.