Skip to content

Instantly share code, notes, and snippets.

@rmflight
Created May 24, 2019 15:10
Show Gist options
  • Select an option

  • Save rmflight/63efb487aaf705dba052a1bdb3b9d451 to your computer and use it in GitHub Desktop.

Select an option

Save rmflight/63efb487aaf705dba052a1bdb3b9d451 to your computer and use it in GitHub Desktop.
fitting and predicting models using just coefficients
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