1

The question I'm trying to answer in R: Load the rockchalk package, and use the plotSlopes() function to visualize the interaction effect by plotting stress by activity, with different slopes for different levels of coffee. Choose 0, 2, and 5 cups of coffee for the slopes, and describe/contrast the effect of activity on stress among heavy (5), moderate (2), and non-drinkers of coffee (0). Be sure to paste your plot into the document, along with the syntax.

This is the code I wrote:

library(rockchalk)
mod1 <- lm(stress ~ activity + coffee*activity, data = `employeeExercise(2).(2)`)
summary(mod1)
plotSlopes(mod1, plotx = "stress", modx = "coffee", modxVals = c(0, 2, 5))

And this is the error I keep getting???: Error in mm[, plotx] : subscript out of bounds​

Here is some sample data:

> head(employeeExercise_3_, 20)
# A tibble: 20 x 1
age male coffee activity preStress heart stress                                         
 1 25 0 3 9 69 0 53                                 
 2 32 1 3 5 66 1 61                                 
 3 35 1 3 3 71 0 55                                 
 4 27 0 1 6 61 0 50                                 
 5 21 1 3 4 61 0 41                                 
 6 53 0 2 4 54 1 85                                 
 7 53 0 4 8 76 1 94                                 
 8 49 0 4 3 68 1 80                                 
 9 35 1 2 5 58 0 56                                 
10 34 1 5 8 77 1 82                                 
11 48 1 1 8 64 0 67                                 
12 31 1 1 10 66 1 47                                
13 29 1 1 8 66 0 43                                 
14 28 1 6 4 74 1 54                                 
15 49 1 4 6 65 0 76                                 
16 35 1 4 2 74 1 55                                 
17 42 1 2 0 66 1 72                                 
18 21 1 2 2 72 1 47                                 
19 28 1 5 10 80 0 73                                
20 44 1 2 9 73 0 71
mysteRious
  • 4,102
  • 2
  • 16
  • 36
  • This feels like a homework question. In the future, see general solutions for [subscript out of bounds errors](https://stackoverflow.com/questions/15031338/subscript-out-of-bounds-general-definition-and-solution). You are already plotting stress as a response variable. Read the help documentation for `plotSlopes` by running `?plotSlopes` in your terminal. In the examples you should be able to spot how to properly adjust what you've already written to get it to work. – Anonymous coward Mar 28 '18 at 22:26

2 Answers2

2

Looking at the plotSlopes documentation (use ?plotSlopes in the console), it says this about the plotx argument

Required. Name of one predictor from the fitted model to be plotted on horizontal axis

There you have it. The plotx argument should be a predictor (i.e. something being used to predict) not the response (i.e. something trying to be explained.

I don't know what problem you're trying to solve, but notice changing plotx = "stress" to the other predictor you have used plotx = "activity" does generate a plot!

enter image description here

The error you're getting

Error in mm[, plotx] : subscript out of bounds​

is basically saying "In the object we're expecting to use, we can't find a column with the value of plotx" In your case plotx is "stress". This is because the mm dataframe it refers to only has predictors (not responses) as columns.

It's not a very specific error on how to fix things, but when you're running into problems the documentation is always a good place to start.

If you're confused about what the plotSlopes functionality is for, check out this document and jump to page 7.

Examples of how to use it can also be found at the bottom of the function documentation I mention in my first paragraph.

Good luck!

LachlanO
  • 1,152
  • 8
  • 14
0

The plotSlopes argument "plotx" is looking for one predictor variable from the lm object.

The plotSlopes function is using model$model, which has the coefficients from the lm function used earlier.

I created a fake dataset that mimics your problem and added an explicit interaction variable.

  install.packages ("rockchalk")
  library(rockchalk)
  set.seed(5280)
  foo.data <- data.table (activity = runif (n = 1000, min = 0, max = 3), coffee = rpois (1000, .9)) #create a fake dataset with activity and coffee as predictors
  foo.data$stress <- foo.data$activity * foo.data$coffee + runif(1000, min = 0, max = 2) #create an independent variable "stress" that is a function of coffee and activity 
  foo.data$interaction <- foo.data$coffee * foo.data$activity #explicitly add the interaction of coffee and activity

  plot(foo.data) #examine the data

  foo.model <- lm(stress ~ activity + coffee + interaction, data = foo.data)

  summary(foo.model)

  str(foo.model$model)

  plotSlopes(foo.model, plotx = "interaction", modx = "coffee", modxVals = c(0, 1, 2, 3))
dinman
  • 55
  • 9