I'm working with RStudio. I've searched, but I haven't been able to find anyone with this problem. I'm dealing with a dataset that has many variables, and I've found that the model I've created with interaction terms has a lower R and R squared than the model without the interaction terms. What would you do in this case? Datasets: https://www.kaggle.com/arashnic/fitbit I used sleepDay_merged.csv and dailyActivity_merged.csv
# Installing packages
install.packages("pacman")
pacman::p_load(tidyverse, lubridate, janitor, DataExplorer, venn, e1071, olsrr,ggiraphExtra)
daily_sleep <- read_csv("sleepDay_merged.csv")
head(daily_sleep)
# Cleaning and mutating, data frames, and preparing for merge.
daily_sleep <- daily_sleep %>%
clean_names() %>%
mutate(date = mdy(sleep_day))
daily_activity <- read_csv("dailyActivity_merged.csv")
head(daily_activity)
glimpse(daily_activity)
daily_activity <- daily_activity %>%
clean_names() %>%
mutate(id = as.factor(id), date = mdy(activity_date), day = weekdays(date))
head(daily_activity)
sleep_activity <- merge(daily_activity, daily_sleep)
sleep_activity <- sleep_activity[c(1,2,4,5,8:10,12:16,17,20,21)]
head(sleep_activity)
str(sleep_activity)
usage <- daily_activity %>%
group_by(id,date) %>%
summarise(day,sum_minutes = sum(lightly_active_minutes)+sum(fairly_active_minutes)+
sum(very_active_minutes)) %>%
mutate(usage_level = case_when(
sum_minutes >= 0 & sum_minutes <= 183 ~ "Low Usage",
sum_minutes >138 & sum_minutes <= 358.8 ~ "Moderate Usage",
sum_minutes > 358.8 | sum_minutes <= 552.0 ~ "High Usage"))
head(usage)
summary(usage)
usage_day <- usage %>% group_by(day) %>% summarise(sum_minutes,usage_level)
usage_day$day <- ordered(usage_day$day,
levels=c("Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"))
# Transforming sleep_activity data
sleep_activity %>% plot_histogram(ncol = 5, ggtheme = theme_light())
sleep_activity_filtered <- merge(sleep_activity,usage)
sleep_activity_filtered <- sleep_activity_filtered[!(sleep_activity_filtered$sedentary_minutes <5),]
head(sleep_activity_filtered)
# Transforming skewed data with log transformations
sleep_activity_filtered <- sleep_activity_filtered %>%
mutate(log_moderately_active_distance = log(moderately_active_distance+1),
log_very_active_distance = log(very_active_distance+1),
log_very_active_minutes = log(very_active_minutes+1),
log_fairly_active_minutes = log(fairly_active_minutes+1))
colnames(sleep_activity_filtered)
sleep_activity_filtered <- sleep_activity_filtered %>%
select(-c("moderately_active_distance",
"very_active_minutes",
"very_active_distance",
"fairly_active_minutes"))
# Merging final data frames.
new_data <- sleep_activity_filtered %>%
select(-c("id"))
Regular multiple linear regression model:
multi_model <- lm(calories ~., data = new_data)
summary(multi_model)
mutli_step <- ols_step_both_p(multi_model, pent = 0.05, prem = 0.1, details = TRUE)
Model Summary
-----------------------------------------------------------------
R 0.923 RMSE 292.130
R-Squared 0.851 Coef. Var 12.175
Adj. R-Squared 0.847 MSE 85339.931
Pred R-Squared 0.829 MAE 215.072
Multiple Linear Regression with Interaction terms:
interaction <- lm(calories ~ date + day + sedentary_minutes + (total_minutes_asleep *
total_time_in_bed) + (total_steps*total_distance * light_active_distance *
lightly_active_minutes) + (total_steps * total_distance *
log_moderately_active_distance *
log_fairly_active_minutes) +
(total_steps * total_distance * log_very_active_distance *
log_very_active_minutes), data = new_data )
summary(interaction)
int_step_model <- ols_step_both_p(interaction, pent = 0.5, prem = 0.1, details = TRUE)
step_model$interaction
plot(int_step_model, interaction, print_plot = TRUE)
Model Summary
------------------------------------------------------------------
R 0.871 RMSE 373.020
R-Squared 0.758 Coef. Var 15.546
Adj. R-Squared 0.750 MSE 139143.659
Pred R-Squared 0.736 MAE 293.464
------------------------------------------------------------------