sapply in R not working as expected, why?

Viewed 32

I am running simulations in R. The idea is to see how well some estimators behave for certain true parameters values (2 parameters in total).

The codes for running the estimators:

Myfun = function(N,p1,p2){
  x = roippl(N,p1,p2)
  MLE = ML(x)
  MOME = mome(x)
  OLSE = ols(x,p1,p2)
  WLSE = wls(x,p1,p2)
  CVM = cvm(x,p1,p2)
  MCVM = mcvm(x,p1,p2)
  MPS = mps(x,p1,p2)
  c(MLE,MOME,OLSE,WLSE,CVM,MCVM,MPS)
}

where N is the size of the simulation, p1 and p2 are the true parameters (they also act as a starting values for OLSE to MPS estimators).

I would like to repeat the simulation above for 1000 times, here are the codes:

i = 1000; p1 = 2; p2 = 0.5; N = 1000
t = sapply(i, function(i) Myfun(N,p1,p2))
t

The results are as such:

          [,1]
 [1,] 2.1519743
 [2,] 0.4596518
 [3,] 2.4623396
 [4,] 0.3717018
 [5,] 1.9440906
 [6,] 0.4902122
 [7,] 2.1336764
 [8,] 0.4461416
 [9,] 1.9878930
[10,] 0.4837088
[11,] 1.9794096
[12,] 0.4875101
[13,] 2.0016161
[14,] 0.4821530

where the first two are the estimated parameters for p1 and p2 using ML method, respectively, and so on. Why is there only one column? Shouldn't it gives 1000 columns because there are 1000 iterations in total? Which codes should I fix?

0 Answers
Related