R/fun_lm.R

#' fun_lm
#'
#' @param dependent dependent variable in linear regression
#' @param regressors regressors for the linear regression
#' @param data data in which depend and regressors are
#'
#' @return list with model and tableStats. Model is the output object for lm and tableStat is the t-statistics, P-values and R^2 for the intercept and beta's for the regressrs. OBS: As of nowone can only make a linear regression where intern relation between regressors is not accounted for.
#' @export
#'
fun_lm <- function(dependent, regressors, data) {
  df <- data.frame(data)
  f <- paste(dependent, "~", paste(regressors, collapse = "+"))
  model <- do.call("lm", list(as.formula(f), data = as.name("df")))
  out.df <- cbind(round(summary(model)$coef[,c("t value", "Pr(>|t|)"),drop=F], digits = 2), R2 = c(
    paste(round(summary(model)$r.squared * 100, digits = 2), "%"), ""))
  rownames(out.df) <- c("alpha", paste0("beta_",regressors))
  out <- list(model = model,
              tableStat = out.df)
  return(out)
}
3schwartz/SpecialeScrAndFun documentation built on May 4, 2019, 6:29 a.m.