Skip to content
Snippets Groups Projects
modeling3.R 1.56 KiB
##########################################################################################
################   Gather the information for the response varialbe   ####################
##########################################################################################

getModel <- function(data){
  print('Conducting regression analysis of the data...')
  # This will find the resdiuals that fall within a certain range and return the number of 
  # residuals that are outside of the range and DP with the highest residual
  lm.mod <- lm(y~(.)^2, data = data) 
  print('Model Complete..')
  setwd(Dir)
  print('Saving model to folder...')
  save(lm.mod, file = 'lnmod.RData')
  # https://stackoverflow.com/questions/14761496/saving-and-loading-a-model-in-r
  print('Saving data frame to folder..')
  write.table(data, file = 'model data.csv', sep = ',')
  # https://datascienceplus.com/exporting-data-from-r/

  avg <- mean(lm.mod$residuals)
  sigma <- sd(lm.mod$residuals)
  
  ub <- avg + 2*sigma 
  lb <- avg - 2*sigma

  resid <- sum(lm.mod$residuals>ub) + sum(lm.mod$residuals<lb)
  percent <- resid/nrow(data)
  print(paste0('The number of residuals outside of the accepted range is ',resid,'.'))
  print(paste0('The current length of the DOE is ', nrow(data),'.'))
  print(paste0('The percentage of residuals outside of the accepted range is ', round(percent,5), '.'))
  newCen <- which.max(abs(lm.mod$residuals))
  print(paste0('Building new DOE using design point ', names(lm.mod$residuals[newCen]),'.'))
  print('Modeling complete...')
  return(c(percent,newCen, plot(lm.mod)))
}