R/LR_Statistics.R

Defines functions LR_Statistics

Documented in LR_Statistics

#'@title A function that run linear regression over the model matrix
#'@description A function that calculate the adjusted R squared of a linear regression for RV prediction.
#'
#'@param MM a matrix of the model matrix.
#'
#'@return a 3*4 matrix of 3 statistics correspond to 2 different designs.
#'@export
LR_Statistics <- function(MM){
  return_matrix = matrix(NA,nrow = 3, ncol = 4)
  rownames(return_matrix) = c("coef_est","pvalue","adj_Rsquared")
  colnames(return_matrix) = c("V_nof","TN_nof","V_filter","TN_filter")

  MM = MM[,colnames(MM) != "X"]
  indx_basic = grepl("RV_.*|Monday|Y",colnames(MM))
  MM = na.omit(MM)
  MM = as.data.frame(scale(MM))

  mod_V_nof <- summary(lm(Y~.,data = MM[,indx_basic|colnames(MM) == "V_raw"]))
  return_matrix[1,1] <- mod_V_nof$coefficients["V_raw",1]
  return_matrix[2,1] <- mod_V_nof$coefficients["V_raw",4]
  return_matrix[3,1] <- mod_V_nof$adj.r.squared

  mod_TN_nof <- summary(lm(Y~.,data = MM[,indx_basic|colnames(MM) == "TN_raw"]))
  return_matrix[1,2] <- mod_TN_nof$coefficients["TN_raw",1]
  return_matrix[2,2] <- mod_TN_nof$coefficients["TN_raw",4]
  return_matrix[3,2] <- mod_TN_nof$adj.r.squared

  mod_V_fw <- summary(lm(Y~.,data = MM[,indx_basic|colnames(MM) == "V_weekly_stl"]))
  return_matrix[1,3] <- mod_V_fw$coefficients["V_weekly_stl",1]
  return_matrix[2,3] <- mod_V_fw$coefficients["V_weekly_stl",4]
  return_matrix[3,3] <- mod_V_fw$adj.r.squared

  mod_TN_fw <- summary(lm(Y~.,data = MM[,indx_basic|colnames(MM) == "TN_weekly_stl"]))
  return_matrix[1,4] <- mod_TN_fw$coefficients["TN_weekly_stl",1]
  return_matrix[2,4] <- mod_TN_fw$coefficients["TN_weekly_stl",4]
  return_matrix[3,4] <- mod_TN_fw$adj.r.squared

  return(return_matrix)
}
ZhenWei10/Sherry-Chapter1 documentation built on Oct. 31, 2019, 1:48 a.m.