Skip to content

Instantly share code, notes, and snippets.

@Jfortin1
Last active August 29, 2015 14:20
Show Gist options
  • Save Jfortin1/e3a4ca3c3c804a0b8e60 to your computer and use it in GitHub Desktop.
Save Jfortin1/e3a4ca3c3c804a0b8e60 to your computer and use it in GitHub Desktop.
getR2.R
getR2 <- function(Y,X){
fitted <- t(solve(t(X)%*%X) %*% t(X) %*% t(Y)) %*% t(X)
res <- Y-fitted
Y.demeaned <- t(scale(t(Y), center=TRUE, scale=FALSE))
ssres <- rowSums(res^2)
sstot <- rowSums(Y.demeaned^2)
1-ssres/sstot
}
getAdjustedR2 <- function(Y,X){
fitted <- t(solve(t(X)%*%X) %*% t(X) %*% t(Y)) %*% t(X)
res <- Y-fitted
Y.demeaned <- t(scale(t(Y), center=TRUE, scale=FALSE))
ssres <- rowSums(res^2)
sstot <- rowSums(Y.demeaned^2)
R2 <- 1-ssres/sstot
p <- ncol(X)-1
n <- nrow(X)
adjusted <- R2-(1-R2)*(p/(n-p-1))
adjusted
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment