2

I want to obtain four slopes for piecewise regression. Two slopes for each release type before 365 days, and after 365 days. I also know I should use the emmeans package.

Here is a dummy dataset.

 df <- data.frame (tsr = c(0,0,9,10,19,20,20,21, 30,30,100,101,200,205,350,360, 400,401,500,501,600,605,700,710,800,801,900,902,1000,1001,1100,1105,2000,2250,2500,2501),
              release_type = c('S','H','S','H','S','S','H','S','H','S','S','H','S','H','S','S','H','S','H','S','S','H','S','H','S','S','H','S','H','S','S','H','S','H','S', 'H'),
              cond = c(250,251,250,251,300,301,351,375,250,249,216,257,264,216,250,251,250,251,300,301,351,375,250,249,216,257,264,216, 250,251,250,251,300,301,351,375),
              notch = c('A','B','C','D','A','B','C','D','A','B','C','D','E','G','E','G','A','H','J','K','L','Q','W','E','R','Y','U','I','O','P','Y','U','I','O','P', 'Z'))

 #Load libraries
 library(emmeans)
 library(lme4)

 #Set up break point manually
 bp = 365
 b1 <- function(x, bp) ifelse(x < bp, bp - x, 0)
 b2 <- function(x, bp) ifelse(x < bp, 0, x - bp)
 
 #Fit linear mixed effect model using piecewise regression
 m1 <- lmer(cond~b1(tsr, bp) + b2(tsr,bp) + b1(tsr, bp):release_type 
       + b2(tsr,bp):release_type + release_type + (1|notch), data = df)

 #Obtain slopes
 emtrends(m1, params = "bp", var = "tsr", pairwise ~ release_type)

I am only getting estimates for one slope of each release type. What am I doing wrong?

Note: I cannot use the summary() function to obtain slopes because it uses the function above to generate those estimates. So it is not the pure slopes.

Rnoobie
  • 139
  • 7
  • In `b1()` you have `bp - x` while in `b2()` you have `x - bp`, so one is decreasing in x and one is increasing in x. Is that really the way you want it? With slopes that are negative of the other in each piece? Also, are you sure you can't add together values from `summary()` to find the slopes? – DavidLukeThiessen Jun 07 '22 at 13:02

1 Answers1

1

You have to add at = list(tsr = c(10, 400)) to the emtrends() call to specify representative times before and after the breakpoint. Otherwise, it just uses the average value of tsr since it is a quantitative predictor.

Russ Lenth
  • 5,922
  • 2
  • 13
  • 21