0
$\begingroup$

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 
------------------------------------------------------------------
$\endgroup$
4
  • $\begingroup$ Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. $\endgroup$
    – Community
    Commented Sep 11, 2021 at 18:06
  • $\begingroup$ I can't trim the code and get the same result. I want to know what others would do in this same situation. $\endgroup$
    – Nini
    Commented Sep 11, 2021 at 18:07
  • 1
    $\begingroup$ You are not comparing a model with / without interaction terms. You are comparing models that have selected variables by stepwise selection. I'd suggest that you don't use stepwise selection. $\endgroup$
    – user20650
    Commented Sep 11, 2021 at 18:33
  • $\begingroup$ Thank you for responding user20650. I don't really understand what you mean by me not comparing two models with and without interaction terms. The second model has interaction terms. Oh, I think I see what you mean. The set of summary statistics for the first model has a lower R and R squared when I don't use stepwise. But I figured that stepwise would choose the most efficient model. $\endgroup$
    – Nini
    Commented Sep 11, 2021 at 18:37

1 Answer 1

3
$\begingroup$

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.

This is completely unsurprising since more variables will reduce the RSS, even if they are not truly associated with the outcome.

What would you do in this case?

Its a bit late to do this now, but if you're going to compare models like this you need to pre-specify the comparison and then perform an F test between the two models.

As of now, you're doing something like stepwise regression, and stepwise is known to be a poor method for model selection let alone inference. I would encourage you to search this forum for mentions of stepwise and read some of the more popular answers.

$\endgroup$
3
  • 1
    $\begingroup$ Wouldn’t what the OP described mean that the residual sum of squares is larger for the model with the interaction term? $\endgroup$
    – Dave
    Commented Sep 11, 2021 at 22:10
  • $\begingroup$ Hi Demetri. What do you mean by pre-specify my comparison? I did notice that without using stepwise, the R and R squared are larger for the model with the interaction terms, and the F statistic is smaller, as well as the standard errors. However, the final model includes more variables than the one chosen by stepwise. I didn't realize that stepwise is not a preferred method. $\endgroup$
    – Nini
    Commented Sep 11, 2021 at 22:35
  • $\begingroup$ Without interaction terms: Residual standard error: 292.4 on 386 degrees of freedom Multiple R-squared: 0.8545, Adjusted R-squared: 0.8465 F-statistic: 107.9 on 21 and 386 DF, p-value: < 2.2e-16 With interaction terms: Residual standard error: 263.5 on 357 degrees of freedom Multiple R-squared: 0.8907, Adjusted R-squared: 0.8754 F-statistic: 58.19 on 50 and 357 DF, p-value: < 2.2e-16 $\endgroup$
    – Nini
    Commented Sep 11, 2021 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.