问题
I am using package segmented in R. I first fitted a Poisson glm with a log link function using the glm
function that includes an offset term like:
M1=glm(Y~X1+X2+X3+offset(log(X)),data=dat.1,family=poisson)
M1 is fitted without any error. Next I tried to fit a segmented glm
by using the package segmented in R as:
library(segmented)
seg.1=segmented(M1,seg.Z=~X1,psi=list(X1=c(0.5)))
I am having the following error:
Error in offset(log(X)) : object 'X' not found
Where is my mistake here? Thanks a lot.
回答1:
Explicitly providing the location of X
(e.g. sample_dat$X
) in the call to glm
does the trick.
Here is a reproducible example:
library(segmented)
# sample data
set.seed(101)
sample_dat <- data.frame(Y = rpois(100, 3), X1 = rnorm(100), X2 = rnorm(100), X = rpois(100, 100))
# fit model
M1 = glm(Y ~ X1 + X2, data = sample_dat, offset = log(sample_dat$X), family = poisson)
seg.1 = segmented(M1, seg.Z=~X1, psi=list(X1 = c(0.5)), data = sample_dat)
来源:https://stackoverflow.com/questions/19034403/r-package-segmented-with-an-offset-term