# standard r-squared with y and yhat as vectors
f <- stats::lm(mpg ~ cyl + disp + hp + wt, data = datasets::mtcars)
y <- f$model$mpg
yhat <- f$fitted.values
calc_rsquared(y = y, yhat = yhat)
# standard r-squared with y as a matrix and yhat containing 'x' and linear coefficients
s <- summary(f)
x <- cbind(1, as.matrix(f$model[, -1]))
w <- matrix(s$coefficients[, 1], ncol = 1)
calc_rsquared(y = matrix(y, ncol = 1), yhat = list(x, w))
# multivariate r-squared with y and yhat as matrices
calc_rsquared(y = cbind(y, y), yhat = cbind(yhat, yhat))
# multivariate r-squared with yhat as a linear reconstruction of two matrices
calc_rsquared(y = cbind(y, y), yhat = list(x, cbind(w,w)))
Run the code above in your browser using DataLab