通过泊松回归生成数据样本


13

我想知道如何从R中的泊松回归方程生成数据?我对如何解决这个问题感到困惑。

因此,如果我假设我们有两个分布为预测变量和。截距为0,两个系数都等于1。那么我的估计很简单:X1X2N(0,1)

log(Y)=0+1X1+1X2

但是,一旦计算出log(Y),如何基于该值生成泊松计数?泊松分布的速率参数是多少?

如果有人可以编写一个简短的R脚本来生成泊松回归样本,那将太棒了!

Answers:


24

泊松回归模型假定的泊松分布,并使用链接函数。因此,对于单个解释变量,假设(因此),并且。根据该模型生成数据很容易。这是一个示例,您可以根据自己的情况进行调整。日志X ý P μ ë Ý = V Ý = μ 日志μ = β 0 + β 1 XYlogxYP(μ)E(Y)=V(Y)=μlog(μ)=β0+β1x

>   #sample size
> n <- 10
>   #regression coefficients
> beta0 <- 1
> beta1 <- 0.2
>   #generate covariate values
> x <- runif(n=n, min=0, max=1.5)
>   #compute mu's
> mu <- exp(beta0 + beta1 * x)
>   #generate Y-values
> y <- rpois(n=n, lambda=mu)
>   #data set
> data <- data.frame(y=y, x=x)
> data
   y         x
1  4 1.2575652
2  3 0.9213477
3  3 0.8093336
4  4 0.6234518
5  4 0.8801471
6  8 1.2961688
7  2 0.1676094
8  2 1.1278965
9  1 1.1642033
10 4 0.2830910

3

如果您想生成一个完全适合模型的数据集,则可以执行以下操作R

# y <- exp(B0 + B1 * x1 + B2 * x2)

set.seed(1234)

B0 <-  1.2                # intercept
B1 <-  1.5                # slope for x1
B2 <- -0.5                # slope for x2

y <- rpois(100, 6.5)

x2 <- seq(-0.5, 0.5,,length(y))
x1 <- (log(y) - B0 - B2 * x2) / B1

my.model <- glm(y ~ x1 + x2, family = poisson(link = log))
summary(my.model)

哪个返回:

Call:
glm(formula = y ~ x1 + x2, family = poisson(link = log))

Deviance Residuals: 
       Min          1Q      Median          3Q         Max  
-2.581e-08  -1.490e-08   0.000e+00   0.000e+00   4.215e-08  

Coefficients:
            Estimate Std. Error z value Pr(>|z|)    
(Intercept)  1.20000    0.08386  14.309  < 2e-16 ***
x1           1.50000    0.16839   8.908  < 2e-16 ***
x2          -0.50000    0.14957  -3.343 0.000829 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1   1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 8.8619e+01  on 99  degrees of freedom
Residual deviance: 1.1102e-14  on 97  degrees of freedom
AIC: 362.47

Number of Fisher Scoring iterations: 3
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.