Dear R-List,
I have data that is collected in panels like this (output from the
dput() function, the first 20 observations in the data set):
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), .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), Month = c(1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8), Yes = c(21L,
18L, 20L, 19L, 31L, 39L, 28L, 2L, 28L, 21L, 26L, 50L, 14L, 28L,
50L, 83L, 10L, 25L, 22L, 6L), No = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 4L, 0L, 0L, 0L, 0L), Abstention =
c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 3L, 0L,
0L, 0L, 0L), No.Neg = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Abstention.Neg = c(0L,
0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 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), Yes.Pos = c(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), Enlargement = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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(NA,
20L), class = "data.frame")
The data set has in total 27 countries for the years 1999 to 2008, but
with unbalanced panels.
I want to be able to estimate a model and do forecasting for each
country in the data set. I have been looking into the YourCast package
from King et al. but since I have all my data in a single file, I am
at a loss as to how to create a data object that the yourcast()
function will accept. Does anyone know how to do this without going
through the tedious procedure of manually splitting the data file up
into the different cross sections?
Best, Thomas
YourCast Data Format
2 messages · Thomas Jensen, Dieter Menne
1 day later
Thomas Jensen-6 wrote:
... data set ... The data set has in total 27 countries for the years 1999 to 2008, but with unbalanced panels. I want to be able to estimate a model and do forecasting for each country in the data set. I have been looking into the YourCast package from King et al. but since I have all my data in a single file, I am at a loss as to how to create a data object that the yourcast() function will accept.
The base R-method uses "by" followed by "do.call":
dt = your data structure in the mail which has only one country, so the
result is a bit confusing
dt.by = by(dt,dt$Country, function(x){
# put you own calculation here
data.frame(Absention.neg=mean(x$Abstention.Neg),
Absention.neg=mean(x$Abstention.Neg))
})
do.call("rbind",dt.by)
This sequence is not really intuitive, so an add-on industry has evolved,
for examples in packages doBy (fast, straightforward) and plyr (can be slow,
but comprehensive and consistent). Best is you try the base method first,
and work with the packages later.
Dieter
View this message in context: http://r.789695.n4.nabble.com/YourCast-Data-Format-tp3205174p3206697.html Sent from the R help mailing list archive at Nabble.com.