繁体   English   中英

如何将泊松回归的系数约束为R?

[英]How to constrain the coefficients of a Poisson regression to be positive in R?

假设以下情况:
我有一个变量Y的数据,我假设它是泊松分布的。 我在同一时间段内也有变量X的数据,每个观察代表一个特定的事件。 我假设Y的值来自两个不同的影响,所以我将每个观测Y_i分成两个泊松分布的Y_i1和Y_i2,但我仍然只有关于总Y_i的观测值。 我还假设事件(由X表示)对Y_i1有长期影响,我有参数lambda_i2的估计量。

所以我的回归公式是
fml=Y_i ~ b_1*X_i+....+b_n*X_(i-n+1) + offset(lambda_i2) -1 ,n> = 24。
这意味着X的最后24个(或更多,因为长期效应)值以加法的方式影响Y_i1的值,并且我没有截距(b_0 = 0)。

我做了一个矩阵m,其行代表Y_i,它的所有24个(或更多)回归量用于Y_i的每次观察和lambda_i2的相应估计。

现在我使用glm(fml, family=poisson(link="identity"), data=m)并尝试使用不同的n(= 24,48,36,...)值。

总是,一些系数得到的负值在解释中没有意义。 (由X表示的事件对Y的值只能有正面影响或没有影响。)

这引出了我的问题:

如何在模型中使用约束b_i >=0

在我以前的研究中,我发现函数glmc(),但我不知道如何在这里包含我的约束。

作为替代方案,我也考虑过以贝叶斯方式分析这个模型,但是我没有找到Poisson分布的贝叶斯版本的glm(),这样我就可以自己指定b_i的先验。 (然后我可以在前面加入积极性。)

你有什么想法?

这是我的数据和代码的摘录:

y=c(279,623, 1025, 1701, 1862, 2544, 2308, 2231, 2234, 2550, 2698, 2805, 3510, 3032, 2746, 2074, 1062,  513,  226,  116,   87,   79,  116, 335,  594, 1081, 1425, 1775, 2056, 2387, 2337, 2354, 2665, 2406, 2433, 2550, 2820, 3655, 4566, 2330, 1267,  531,  280,  148,   92,   89, 141,  458,  852, 1214)
X=c(0, 0, 0,  0,  0,  0, 0, 0,  0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.88349136, 0.54951680, 0.13306912, 0.15321180, 0.00000000, 1.42569128, 0.55808054, 0.65486418, 0.27530564, 0.24813572, 0, 0, 2.09889028, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1.18947898, 0.17347032, 0.94538886, 0.03334654, 0.05593732, 0.00000000, 0.99772264, 0.11121918, 0, 1.41673120, 0.27375384, 0, 0, 0, 0, 5.67487576, 0, 0, 0, 0, 0, 0, 0, 0, 1.55642510, 0.98419866, 0.50992652)
lambda=c(253.5,  562.5, 1053.0, 1645.0, 2064.5, 2215.0, 2503.0, 2443.0, 2514.5, 2701.0, 2972.5, 3035.5, 3422.5, 3295.0, 2882.5, 2094.0, 1211.0,  579.5,  265.5,  155.0,  112.5,   82.5,  117.5,  306.0,  627.0, 1021.0, 1463.5, 1722.5, 2017.5, 2146.5, 2209.0, 2231.5, 2265.0, 2320.0, 2442.0, 2507.0, 2957.0, 3674.0, 3345.5, 2285.0, 1265.0,  555.5,  252.0,  145.5,   86.5,   90.5,  148.0,  362.0, 738.0, 1137.5)

regressors=function(n,x){
  m=length(x)-n+1;
  r=matrix(0,m,n);
  for (i in 0:(n-1)){ r[,(i+1)]=x[(n-i):(length(x)-i)]}
  return(r);
}
r=regressors(24,X);
reg=cbind(y,data.frame(r),lambda);
fml=as.formula(paste("y~", paste(colnames(reg)[2:25], collapse = "+"), "+offset(lambda)-1"));
g=glm(fml, poisson(link="identity"), data=reg); %this leads to negative coefficients
obj=function(b){-sum(dpois(y, r%*%b, log=TRUE))}
st=coef(lm(fml, data=reg));
opt=optim(st, obj); % this is where the error occurs

regressors()是我为计算回归量而编写的函数(它产生一个矩阵r,其中n列和50行,每行i代表y_i的回归量)。

尝试第一原则。

# generate random input data
set.seed(123)
n <- 100
x <- 1:n
X <- cbind(1, x)
b <- c(0.1, 3)
y <- rpois(n, X %*% b)

# log likelihood objective function
obj <- function(b) -sum(dpois(y, X %*% b, log = TRUE))

# as a check try with no constraints - these two should give the same coefs
glm(y ~ X + 0, family = poisson("identity"))
st <- coef(lm(y ~ X + 0)); optim(st, obj)

# now add lower bounds ensuring starting value is in feasible region
optim(pmax(st, 1), obj, lower = c(0, 0), method = "L-BFGS-B")

注1:如果您的参数估计值位于可行区域的边界上,请注意上述约束示例中出现的情况。

注意2:这是对稍后添加到问题中的代码的重新处理。 yXlambdaregressors量就像问题一样。 注意添加约束会强制许多系数为零。

r <- regressors(24,X)
reg <- cbind(y,data.frame(r),lambda)

fml <- y ~ . - lambda + offset(lambda)

# check that g and opt give the same coefs

g <- glm(fml, poisson(link = "identity"), data = reg)

obj <- function(b)-sum(dpois(y, r%*%b + lambda, log = TRUE))
st <- coef(lm(fml, data=reg))
opt <- optim(st, obj, method = "BFGS", control = list(maxit = 500))

optim(pmax(coef(g), 1), obj, method = "L-BFGS-B", lower = 0 * st)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM