Skip to content

Instantly share code, notes, and snippets.

@alstat
Created February 6, 2012 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alstat/1754628 to your computer and use it in GitHub Desktop.
Save alstat/1754628 to your computer and use it in GitHub Desktop.
#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. :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment