1

I am studying cumulative weight loss in honey bee hives. I have a dataset with cumulative weight running average (3days), id of the hive, ndate (day in Unix timestamp) and day (in POSIXct timestamp). I used map() to apply a linear model for each hive:

lm.fit <- dt%>% 
  group_by(id) %>% 
  nest() %>% 
  mutate(lmMod = map(data, ~lm(data = ., weight ~ ndate)),
         coeffs = map(lmMod, coefficients),
         slope = map_dbl(coeffs, 2))

The result it's a list with a linear model, coefficenint and slope for each hive, and the data used for each model.

I would like now to apply for each linear model the function segmented() from the Segmented Package. My purpose is to use the segmented function to find 2 breakpoints (previously observed by plotting the data) for each hive.

example Segmented function:

model <- segmented(lm(y ~ x), seg.Z = ~x, psi = 50)

#lm(y~x) --> I calculated them previously with map
#~x --> ndate (from the original data frame)
#psi = he initial guess for the breakpoints

I have been trying again with map and lapply, but unsuccesfully so far. How would you proceed? Any suggestion? Thank you for the help.

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 21 '23 at 15:31

0 Answers0