for loop and generating random panel data

89 Views Asked by At

I want to generate a panel data sets with 5 units and 10 observations in r and get the test statistics. However, my problem here is to do this process 1000 times. For each id I need to restart the cumsum argument again for randomness. I want to create 1000 random data sets by providing randomness in each unit separately and apply adf.test to them 1000 times. Does anyone have any ideas?

library(tseries)
set.seed(123)
result <- NULL
for (i in 1:1000) {
  n_entities <- 5
  n_years <- 10
  id <- rep(1:n_entities, each = n_years)
  year <- rep(2000:2009, times = n_entities)
  value <- cumsum(rnorm(n_entities * n_years))
  data <- data.frame(id = id, Year = year, Value = value)
  result [i] = adf.test(data$Value)$statistic
}

I tried to edit it with different codes, but I realized that the data set does not get the cumulative sum for each unit (n_entities) again. I need to generate a cumulative random data set for each unit in turn, rather than setting the size and filling it with data.

1

There are 1 best solutions below

2
AkselA On

Using Allan's suggestion, and cleaning up a little

set.seed(123)
n_entities <- 5
n_years <- 10
id <- rep(1:n_entities, each = n_years)
year <- rep(2000+(1:n_years)-1, times = n_entities)

result <- replicate(100, {
    value <- c(replicate(n_entities, cumsum(rnorm(n_years))))
    data.frame(id = id, Year = year, Value = value)
}, simplify=FALSE)

lapply(result[1:2], "[", 8:12, 1:3)
# [[1]]
#    id Year      Value
# 8   1 2007 1.87877126
# 9   1 2008 1.19191841
# 10  1 2009 0.74625644
# 11  2 2000 1.22408180
# 12  2 2001 1.58389562
# 
# [[2]]
#    id Year       Value
# 8   1 2007  1.87706415
# 9   1 2008  2.00091839
# 10  1 2009  2.21685996
# 11  2 2000  0.37963948
# 12  2 2001 -0.12268397
# 

adf_stat <- sapply(result, function(x) adf.test(x$Value)$statistic)
adf_stat[1:4]
# Dickey-Fuller Dickey-Fuller Dickey-Fuller Dickey-Fuller 
#    -2.6577763    -1.5253793    -1.4580183    -2.8355005 

For a simple function that does this basic thing…

adf_sim <- function(n_entities=5, n_years=10, reps=1e2, year1=2000){
    id <- rep(1:n_entities, each=n_years)
    year <- rep(year1+(1:n_years)-1, times=n_entities)
    replicate(reps, {
        adf.test(c(replicate(n_entities, cumsum(rnorm(n_years)))))$statistic
    })
}

set.seed(123)
adf_sim()[1:4]
# Dickey-Fuller Dickey-Fuller Dickey-Fuller Dickey-Fuller 
#    -2.6577763    -1.5253793    -1.4580183    -2.8355005