如何为不平衡面板数据生成Halton序列

时间:2019-07-20 06:49:01

标签: r statistics

每个人。我想为不平衡的面板数据生成一个Halton序列。但是我只能为平衡的面板生成一个Halton序列。我想知道是否能给我一些帮助。

我用于平衡面板数据的代码如下:

nh <- 200
hgrid <- halton(nh * length(y) / nt, dim = 1, normal = TRUE)
htmat <- matrix(hgrid, length(y) / nt, nh, byrow = TRUE)
htmat <- htmat %x% rep(1, nt)

nh是哈尔顿点数; y是因变量; nt是面板数据的周期; 在平衡面板数据中,任何一个人的两个周期都是相同的,因此我可以首先为每个人生成希尔顿积分,然后将每个人的积分重复两次。

但是在不平衡面板中,每个人的时期都不相同。如何将其扩展到不平衡面板数据? 我想我们可以为每个id生成halton序列,并将其复制nt次(nt是每个人出现的次数),然后可以通过id合并这两个矩阵。

data('Health',package = 'Rchoice')
attach(Health)
# generate nt for each id
nt <- rle( sort( Health$id ) )
Health$nt <- nt[[1]][ match( Health$id , nt[[2]] )]

#generate halton sequence for each id
library(randtoolbox)
nh <- 20
idmax <- 7293
hgrid <- halton(nh * idmax, dim = 1, normal = TRUE)
htmat <- matrix(hgrid,idmax, nh, byrow = TRUE)

但是我不知道如何为每个id重复此序列nt次,以及如何合并这两个矩阵。在此先感谢

0 个答案:

没有答案