0

我有这样的面板中组织的数据(请参阅下面的dput()函数输出):

Country Year Month Var1 Var2
C1      2000 1     0    0
C1      2000 2     1    0
C1      2000 3     2    1
...  
C2      2000 1     1    1
C2      2000 2     1    2
C2      2000 3     3    1
...

从 1999 年到 2008 年,该数据集共有 27 个国家,但面板不平衡。

我希望能够为整个数据集估计一个模型,并从这个模型对数据集中的每个国家进行预测。我一直在研究 King 等人的 YourCast 包。但是由于我将所有数据都放在一个文件中,因此我不知道如何创建 yourcast() 函数将接受的数据对象。有谁知道如何做到这一点,而无需经历手动将数据文件拆分为不同横截面的繁琐过程?

PS:来自数据集的 48 个观察值:

structure(list(Country = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Belgium", 
"Denmark", "Czech.Republic", "Germany", "Estonia", "Greece", 
"Spain", "France", "Ireland", "Italy", "Cyprus", "Latvia", "Lithuania", 
"Luxembourg", "Hungary", "Malta", "Netherlands", "Austria", "Poland", 
"Portugal", "Slovenia", "Slovakia", "Bulgaria", "Romania", "Finland", 
"Sweden", "UK"), class = "factor"), Year = c(2003, 2003, 2003, 
2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2004, 2004, 
2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2003, 
2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 2003, 
2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 
2004, 2005), Month = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8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 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Yes.Neg = c(1L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 
0L, 0L, 2L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L
), Yes.Pos = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L), Missing = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Enlargement = c(0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1)), .Names = c("Country", "Year", "Month", "Yes", 
"No", "Abstention", "No.Neg", "Abstention.Neg", "Yes.Neg", "Yes.Pos", 
"Missing", "Enlargement"), row.names = c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 
81L, 82L, 83L, 84L, 85L), class = "data.frame")
4

2 回答 2

1

这很简单

图书馆(你的演员);

演示(chp.11.10)

您可以使用 yourprep 命令准备数据,使其看起来像本演示中使用的数据。输入 ?yourprep

于 2011-01-10T20:37:14.863 回答
0

如果我了解您的问题,那么拆分数据库可能会很容易。假设您将数据集命名为“数据”:

results <- list()
for (i in 1:nlevels(data$Country)) {
    results[[levels(data$Country)[i]]] <- yourcast(...)
}

在哪个简单的循环中,您可以对每个国家/地区进行所有预测,并将结果保存到列表中。稍后您可以从results列表中读取所有国家/地区的所有结果。例如:results[['Hungary']]

由于我对您使用的包一无所知,这里有一个小例子,可以安装在循环中,而不是包含 yourcast() 函数的行:

results[[levels(data$Country)[i]]] <- c(levels(data$Country)[i], length(which(data$Country == levels(data$Country)[i])))

哪个命令将创建一个包含所有国家的列表,其中包含两个变量:给定国家的名称和样本大小。

于 2011-01-08T18:17:44.977 回答