Including Trust Interval in a Row of a Matrix

Asked

Viewed 91 times

1

I want to create an array of 38 rows and two columns with coefficients on odd lines and confidence intervals for parameters on even lines.

Then call the Stargazer.

My code is:

library(quantreg)

y <- rnorm(200)
x <- matrix(rnorm(123),200)
fit <- rq(y~x,tau=seq(0.05, 0.95, by = 0.05),method="br")

summary(fit,se="nid")

I made it this far:

Note that I can put the coefficients on the odd lines without problems. The obvious error is when I try to build and put the confidence interval of the parameters on even lines:

fit_Matrix<-matrix(0,38,2)

for(i in 1:19) for (j in seq(1, 38, by = 2)[i]) for (k in seq(2, 38, by = 2)[i]) { 

fit_Matrix[j,1]<-summary(fit,se="nid")[[i]]$coefficients[1]
fit_Matrix[j,2]<-summary(fit,se="nid")[[i]]$coefficients[2]

fit_Matrix[k,1]<-summary(fit,se="nid")[[i]]$coefficients[1]-1.645*summary(fit,se="nid")[[i]]$coefficients[3]; 
summary(fit,se="nid")[[i]]$coefficients[1]+1.645*summary(fit,se="nid")[[i]]$coefficients[3]

fit_Matrix[k,2]<-summary(fit,se="nid")[[i]]$coefficients[2]-1.645*summary(fit,se="nid")[[1]]$coefficients[4];
summary(fit,se="nid")[[i]]$coefficients[2]-1.645*summary(fit,se="nid")[[i]]$coefficients[4]

}

The second part of the code is wrong. How do I fix it? Remembering that I would like to transport the matrix to Latex.

Thank you, from now on.

  • 1

    Your regression has two parameters. Thus, I imagine that the odd lines of the fit_Matrix have two numbers: in column 1 goes the intercept and in column 2 goes the angular coefficient. But how to put the confidence intervals, which are composed of two numbers for each parameter, in just one row of the matrix? You want the IC of a parameter only or your fit_Matrix will have to have 4 elements per even line?

  • Is there any way I can do it with 4 elements per even line? It would be possible to put the two interval numbers together in each even line?

  • 1

    There is a way. But your code has a problem: the fit object does not have 19 elements as it should. For me only 14 appear. Make a length(fit) and check this. Then edit the original post so that your code is actually reproducible.

  • Thank you! Truly the lenght is 14. But the information I want is in summary which has 19. Isn’t it? From what I saw 14 is the size of the list: str(fit).

  • I apologize. I had misunderstood a detail of your result. The length is correct.

1 answer

1


Follow my suggestion. My code got a little dirty, slow and ugly, but I’m in a bit of a hurry.

library(quantreg)

y   <- rnorm(200)
x   <- matrix(rnorm(123), 200)
fit <- rq(y~x, tau=seq(0.05, 0.95, by = 0.05), method="br")

summary(fit, se="nid")

fit_Matrix <- matrix(NA, 38, 4)
index.coef <- seq(1, 37, 2)
index.ic   <- seq(2, 38, 2)

fit_Matrix[index.coef, 1] <- fit$coefficients[1, ]
fit_Matrix[index.coef, 3] <- fit$coefficients[2, ]

mult <- 1.645
i    <- 1

for (j in index.ic){

  fit_Matrix[j, 1] <- unlist(summary(fit, se="nid")[[i]][3][1])[1] - mult*unlist(summary(fit, se="nid")[[i]][3][1])[3]
  fit_Matrix[j, 2] <- unlist(summary(fit, se="nid")[[i]][3][1])[1] + mult*unlist(summary(fit, se="nid")[[i]][3][1])[3]
  fit_Matrix[j, 3] <- unlist(summary(fit, se="nid")[[i]][3][1])[2] - mult*unlist(summary(fit, se="nid")[[i]][3][1])[4]
  fit_Matrix[j, 4] <- unlist(summary(fit, se="nid")[[i]][3][1])[2] + mult*unlist(summary(fit, se="nid")[[i]][3][1])[4]

  i <- i+1

}

fit_Matrix

I couldn’t check the accounts, but I think they’re all right.

  • Thanks! You helped too much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.