本文介绍了R包中有偏移项的分段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在R中使用包 segmented .我首先使用glm
函数将Poisson glm与对数链接函数配合使用,该函数包括偏移项,例如:
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正确安装.接下来,我尝试通过使用R中分段的包来拟合分段的glm
:
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)))
我遇到以下错误:
Error in offset(log(X)) : object 'X' not found
我的错误在哪里?非常感谢.
Where is my mistake here? Thanks a lot.
推荐答案
在调用glm
时明确提供X
(例如sample_dat$X
)的位置即可达到目的.
Explicitly providing the location of X
(e.g. sample_dat$X
) in the call to glm
does the trick.
以下是可重现的示例:
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)
这篇关于R包中有偏移项的分段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!