3

I am estimating a GMM model using library(gmm).

n <- 200
x1 <- rnorm(n)
x2 <- rnorm(n)
x3 <- rnorm(n)
x4 <- rnorm(n)
x5 <- rnorm(n)
x6 <- rnorm(n)

xx <- cbind(x1, x2, x3, x4, x5, x6)
fun <- function(betastar, x) {
m1 <- (x[,1] - x[,2]*betastar - x[,3] - x[,4])*x[,5]
m2 <- (x[,1] - x[,2]*betastar - x[,3] - x[,4])*x[,6]
f <- cbind(m1,m2)
return(f)
}

library(gmm)
k <-  gmm(fun, x=xx, 0, optfct="optim", lower = 0, upper = 2, method="Brent")

I want to replicate it B times by bootstrapping my sample xx (with replacement). My scope is to save the standard errors of betastar for each replication and store all of them somewhere. Is there a fast way to do that ? I know there is the library(boot) which in principle should allow me to do that, but I am having an hard time to figure out how, since for using the function gmm I need to specify another function (fun)

EDIT: What the gmm function is doing is minimizing the other function fun with respect to the parameter betastar. All the terms in gmm() define the way gmm works. What I want is to bind betastar (which is a coefficient) and its standard error in an object, for any 1:B replication. They can be recovered by the commands coef(k) and sqrt(k$vcov) I am trying the following

B <- 199  # number of bootstrapping
betak_boot <- rep(NA, 199)
se_betak_boot <- rep(NA, 199)
for (ii in 1:B){
  sample <- (replicate(ii, apply(xx, 2, sample, replace = TRUE)))
  k_cons <- gmm(fun, x=samples, 0, gradv=Dg, optfct="optim", lower = 0, upper = 2, method="Brent")
  betak_boot[ii] <- coef(k_cons)
  se_betak_boot[ii] <- sqrt(k_cons$vcov)
}

I don't know why, I get an error while applying fun, i.e. Error in x[, 1] : incorrect number of dimensions. Indeed, I don't know why sample is

dim(sample)
[1] 200   6   1
Bob
  • 452
  • 6
  • 18
  • what's Dg? What does the 0 do? bind `betastar` in an object, e.g. a list, is that what you mean? – Toby Apr 16 '14 at 11:11
  • Dg is a parameter (gradient) of the function. Does not matter, I removed it. What the `gmm` function is doing is minimizing the other function `fun`, with respect to the parameter `betastar`. All the terms in `gmm( )` define the way `gmm` works. What I want is to bind betastar (which is a coefficient) and its standard error in an object, for any `1:B` replication. Both of them can be recovered by the commands `coef(k)` and `sqrt(k$vcov)` – Bob Apr 16 '14 at 12:06
  • I am trying to write a loop for doing the entire job so to not use `library(boot)` but I am not having good results. I think it will be also (really) slow, if working. Bootstrapping works like this `samples <- replicate(ii, apply(xx, 2, sample, replace = TRUE))`, where `ii` should be the number of times you create new samples with replacement. I took it here [link](http://stackoverflow.com/questions/13516666/fast-function-for-generating-bootstrap-samples-in-matrix-forms-in-r?rq=1) – Bob Apr 16 '14 at 12:34

1 Answers1

1
library(gmm)
set.seed(123)
n <- 200
x1 <- rnorm(n)
x2 <- rnorm(n)
x3 <- rnorm(n)
x4 <- rnorm(n)
x5 <- rnorm(n)
x6 <- rnorm(n)

xx <- cbind(x1, x2, x3, x4, x5, x6)
fun <- function(betastar, x) {
m1 <- (x[,1] - x[,2]*betastar - x[,3] - x[,4])*x[,5]
m2 <- (x[,1] - x[,2]*betastar - x[,3] - x[,4])*x[,6]
f <- cbind(m1,m2)
return(f)
}
ii=4
samples <- replicate(ii, apply(xx, 2, sample, replace = TRUE))

coefk <- rep(0,ii)
sdk <- rep(0,ii)

for (i in 1:ii) {
        xx <- samples[,,i]
        k <-  gmm(fun, x=xx, 0, optfct="optim", lower = 0, upper = 2, method="Brent")
        coefk[i] <- coef(k)
        sdk[i] <- sqrt(k$vcov)[1,1]
 }
Toby
  • 533
  • 4
  • 15
  • may you explain me `samples[,,i]` as the result of apply ? Does that mean that samples is an object which has `i` "versions" and each time I have to use the `ith` of them ? – Bob Apr 16 '14 at 13:09
  • `class(samples)` is `"array"`. What you produce is a 3 dimensional array. the first 2 dimensions, are the dimensions of your sample. The third dimension, are the individual bootstrappings... – Toby Apr 16 '14 at 13:13
  • As I guess. And why did you write `k <- gmm(fun, x=xx, 0, optfct="optim", lower = 0, upper = 2, method="Brent")` above the loop ? And may you confirm the `apply(xx, 2, sample, replace = TRUE)` resample just within each column ? Just having a little doubt about that. Thank you – Bob Apr 16 '14 at 13:19
  • deleted the line. tis not necessary. you are right. your bootstrapping method is a valid selection to do this, yes. – Toby Apr 16 '14 at 13:33
  • Well, I suppose it works. now I have to check how it performs with 4000 observations and 20 variables.. thanks ! – Bob Apr 16 '14 at 13:42
  • you can speed it ob slightly by using the `apply()` family instead of a for loop. – Toby Apr 16 '14 at 14:36
  • well, I was almost close to write the loop on my own. thanks ! – Bob Apr 16 '14 at 15:08