Created
May 24, 2019 15:10
-
-
Save rmflight/63efb487aaf705dba052a1bdb3b9d451 to your computer and use it in GitHub Desktop.
fitting and predicting models using just coefficients
This file contains hidden or 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
| fit_square_root = function(x, y){ | |
| sr_x = sqrt(x) | |
| # assumes you have an intercept, so need to add the ones to the matrix | |
| X = matrix(c(rep(1, length(x)), sr_x), nrow = length(x), ncol = 2, byrow = FALSE) | |
| sr_fit = stats::lm.fit(X, y) | |
| names(sr_fit$coefficients) = NULL | |
| sr_fit$coefficients | |
| } | |
| predict_sr = function(x, coefficients){ | |
| sr_x = sqrt(x) | |
| # assumes there is an intercept term | |
| X = matrix(c(rep(1, length(x)), sr_x), nrow = length(x), ncol = 2, byrow = FALSE) | |
| coef_matrix = matrix(coefficients, nrow = 2, ncol = 1, byrow = TRUE) | |
| out_frequency = X %*% coef_matrix | |
| out_frequency[, 1] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment