0

I am trying to determine how to output a list of the variable names that yield significant (p < 0.05) interactions in a series of regressions.

I have a dataframe that looks like the following:

behavior  condition  attitude1  attitude2  attitude3
1         0          4          5          7
6         1          3          7          2
5         0          2          1          4
3         1          4          2          6

In reality, I have several more attitudes than displayed here. To run several regressions simultaneously and test for interaction terms, I would typically use the following code:

attitudes <- colnames(df[,3:5])
form <- paste("behavior ~ condition*",attitudes)
model <- form %>%
  set_names(attitudes) %>%
  map(~lm(.x, data = df))
map(model, summary)

The output is a list of each of the following regressions:

lm(behavior ~ condition * attitude1)
lm(behavior ~ condition * attitude2)
lm(behavior ~ condition * attitude3) 

I would like to find a way to output a list of all the variable names with a significant condition*attitude interaction. For example, if p<0.05 for attitude1 and attitude3, the output I would be looking for would be:

attitude1, attitude3 

This question is related to what I am trying to do, but it does not show me how I can do this when I am running the models simultaneously using map().

melbez
  • 960
  • 1
  • 13
  • 36

1 Answers1

0

A quick but inelegant way to accomplish your goal:

map_df(model, tidy) %>%
    mutate(model = rep(attitudes, each = num_of_your_predictors+1))

You can then use filter to get all p.value < .05

icu
  • 1
  • What does tidy refer to in map_df()? I'm looking at the documentation but am having trouble figuring it out. – melbez Aug 10 '20 at 19:48
  • `tidy` is a function from `broom` package. It helps you convert model results into a tidy dataframe. – icu Aug 11 '20 at 20:05