tkluis
5/19/2019 - 4:47 PM

R Model Creation and Summary Stats

# Library Dependencies
library(tidyverse)
library(modelr)
library(broom)

# Example Model Logic

model1 <- lm(avgHouseIncome ~ population, data=dfAgg)
model2 <- lm(avgHouseIncome ~ wgtEducation, data=dfAgg)
model3 <- lm(avgHouseIncome ~ population * wgtEducation, data=dfAgg)
model4 <- lm(avgHouseIncome ~ population + wgtEducation, data=dfAgg)

model5 <- rlm(avgHouseIncome ~ population, data=dfAgg)
model6 <- rlm(avgHouseIncome ~ wgtEducation, data=dfAgg)
model7 <- rlm(avgHouseIncome ~ population * wgtEducation, data=dfAgg)
model8 <- rlm(avgHouseIncome ~ population + wgtEducation, data=dfAgg)

model9 <- lm(avgHouseIncome ~ population * wgtEducation + poplation + wgtEducation, data=dfAgg)
model10 <- lm(avgHouseIncome ~ population * wgtEducation + poplation, data=dfAgg)
model11 <- lm(avgHouseIncome ~ population * wgtEducation + wgtEducation, data=dfAgg)

# Combine Model Data

grid1 <- dfAgg %>%
    data_grid(population, wgtEducation) %>%
    gather_predictions(model1, model2, model3, model4)

grid2 <- dfAgg %>%
    data_grid(population, wgtEducation) %>%
    gather_predictions(model5, model6, model7, model8)

grid3 <- dfAgg %>%
    data_grid(population, wgtEducation) %>%
    gather_predictions(model9, model10, mode11)

# Create Model Performance Data Sets

glance1 <- tibble(c(modelNum="model1", glance(model1))
glance2 <- tibble(c(modelNum="model2", glance(model2))
glance3 <- tibble(c(modelNum="model3", glance(model3))
glance4 <- tibble(c(modelNum="model4", glance(model4))
glance5 <- tibble(c(modelNum="model5", glance(model5))
glance6 <- tibble(c(modelNum="model6", glance(model6))
glance7 <- tibble(c(modelNum="model7", glance(model7)) 
glance8 <- tibble(c(modelNum="model8", glance(model8))
glance9 <- tibble(c(modelNum="model9", glance(model9))
glance10 <- tibble(c(modelNum="model10", glance(model10))
glance11 <- tibble(c(modelNum="model11", glance(model11))

glanceAll <- full_join(glance1, glance2, by = modelNum) %>%
    full_join(., glance3, by =modelNum) %>%
    full_join(., glance4, by =modelNum) %>%
    full_join(., glance5, by =modelNum) %>%
    full_join(., glance6, by =modelNum) %>%
    full_join(., glance7, by =modelNum) %>%
    full_join(., glance8, by =modelNum) %>%
    full_join(., glance9, by =modelNum) %>%
    full_join(., glance10, by =modelNum) %>%
    full_join(., glance11, by =modelNum)