library(caret)
## Warning: package 'caret' was built under R version 4.0.3
## Loading required package: lattice
## Loading required package: ggplot2
library(tidymodels)
## Warning: package 'tidymodels' was built under R version 4.0.3
## -- Attaching packages -------------------------------------- tidymodels 0.1.1 --
## v broom 0.7.0 v recipes 0.1.14
## v dials 0.0.9 v rsample 0.0.8
## v dplyr 1.0.2 v tibble 3.0.3
## v infer 0.5.3 v tidyr 1.1.2
## v modeldata 0.1.0 v tune 0.1.1
## v parsnip 0.1.3 v workflows 0.2.1
## v purrr 0.3.4 v yardstick 0.0.7
## Warning: package 'dials' was built under R version 4.0.3
## Warning: package 'infer' was built under R version 4.0.3
## Warning: package 'modeldata' was built under R version 4.0.3
## Warning: package 'parsnip' was built under R version 4.0.3
## Warning: package 'recipes' was built under R version 4.0.3
## Warning: package 'rsample' was built under R version 4.0.3
## Warning: package 'tune' was built under R version 4.0.3
## Warning: package 'workflows' was built under R version 4.0.3
## Warning: package 'yardstick' was built under R version 4.0.3
## -- Conflicts ----------------------------------------- tidymodels_conflicts() --
## x purrr::discard() masks scales::discard()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x purrr::lift() masks caret::lift()
## x yardstick::precision() masks caret::precision()
## x yardstick::recall() masks caret::recall()
## x yardstick::sensitivity() masks caret::sensitivity()
## x yardstick::specificity() masks caret::specificity()
## x recipes::step() masks stats::step()
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v readr 1.3.1 v forcats 0.5.0
## v stringr 1.4.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x readr::col_factor() masks scales::col_factor()
## x purrr::discard() masks scales::discard()
## x dplyr::filter() masks stats::filter()
## x stringr::fixed() masks recipes::fixed()
## x dplyr::lag() masks stats::lag()
## x purrr::lift() masks caret::lift()
## x readr::spec() masks yardstick::spec()
library(anesr)
data("timeseries_2016")
anes16 <- timeseries_2016
rm(timeseries_2016)
clean <- function(x){ifelse(x < 0, NA, x)}
anes16_clean <- anes16 %>%
mutate(across(everything(), clean)) %>%
select(PID = V161158x , Gender = V161342, Income = V161361x, Religion = V161265x,
Education = V161270, Age = V161267, Ideology = V161126, Race = V161310x, P_reg = V161019, GunAccess = V161188, BuildWall = V161196, ObamaMuslim = V162255, Trump_Fav = V161087, Hannity = V161430) %>%
mutate(across(c(PID, Gender, Income, Religion, Education, Ideology, Race, P_reg, GunAccess, BuildWall, ObamaMuslim, Hannity),as.factor)) %>%
drop_na()
set.seed(131313)
split1 <- initial_split(anes16_clean, prop = .7)
Train_Data <- training(split1)
Test_Data<-testing(split1)
anes_recipe <- recipe(Trump_Fav ~ ., data = Train_Data) %>%
step_dummy(PID, Gender, Income, Religion, Education, Ideology, Race,P_reg, BuildWall, ObamaMuslim, GunAccess, Hannity) %>%
step_center(Age, Trump_Fav) %>%
step_scale(Age, Trump_Fav) %>%
prep(training = Train_Data)
baked_train <- bake(anes_recipe, new_data = Train_Data)
baked_test <- bake(anes_recipe, new_data = Test_Data)
set.seed(131313)
trump_lm <- train(
Trump_Fav ~ .,
data = Train_Data,
method = "lm",
trControl = trainControl(method = "cv", number = 5)
)
## Warning in predict.lm(modelFit, newdata): prediction from a rank-deficient fit
## may be misleading
## Warning in predict.lm(modelFit, newdata): prediction from a rank-deficient fit
## may be misleading
## Warning in predict.lm(modelFit, newdata): prediction from a rank-deficient fit
## may be misleading
## Warning in predict.lm(modelFit, newdata): prediction from a rank-deficient fit
## may be misleading
## Warning in predict.lm(modelFit, newdata): prediction from a rank-deficient fit
## may be misleading
trump_lm[["results"]]
## intercept RMSE Rsquared MAE RMSESD RsquaredSD MAESD
## 1 TRUE 22.61495 0.5816795 16.84226 1.279475 0.04982805 1.389257