I am using the 'segmented
' package to find break points in linear regressions in R
library(tidyverse)
library(segmented)
df <- data.frame(x = c(1:10), y = c(1,1,1,1,1,6:10))
lm_model <- lm(y ~ x, data = df)
seg_model <- segmented(obj = lm_model, seg.Z = ~ x)
But if I run the same model within a purrr:map
, segmented fails.
map_test <- df %>%
nest() %>%
mutate(map_lm = map(data, ~lm(y ~ x, data = .)),
param_map_lm = map(map_lm, tidy))
map_lm_model <- map_test[[2]][[1]]
map_seg_model <- segmented(obj = map_lm_model, seg.Z = ~ x)
"Error in is.data.frame(data) : object '.' not found"
When taking the lm obj from the lm extracted from the map output, segmented fails to find the underlying data.
The two linear model objects, appear, however, identical.
What I actually need to do is a more useful map to run lm over multiple sub-sets of a dataframe, then run 'segmented' on the resulting lm.