Quantile regression in R, rearrange data for latex output
Run quantile regression on (let's say) three different tau using lapply:
listdf <- lapply(vector(tau1, tau2, tau3), function(x){summary.rq(rq(Y ~ x + z, tau=x, data=regdf))})
The result is a list with three elements of class 'summary.rq'.
To extract the coefficients from the regression (from summary.rq object), you can typelistdf[[1]]$coefficients
to get coefficients or listdf[[1]]$tau
to get tau.
So to get the coefficients i run:
coeflist <- lapply(listdf, function(x){data.frame(x$coefficients)})
(list of three elements of class 'data.frame')
What I want to do next is to rearrange the data (coeflist
), to get one data frame for each regression variable, where the rows represents the coefficients for the different tau.
I run: lapply(1:nrow(coeflist[[1]]), functi开发者_运维问答on(i) do.call(rbind, lapply(coeflist, "[", i, TRUE)))
This kind of return what i want, but is there a way to also rename the rownames in the data frames to reflect which tau the coefficients come from? and also rename the list elements to the names of the regression variables?
regards.
First I'd note that the tau argument to rq() accepts vectors, so I'm not sure all your lapply() work is really necessary.
But using example data from the quantreg package, is this sort of what you're looking for:
data(stackloss)
temp <- summary(rq(y ~ x, method="fn",tau=c(0.3,0.5,0.7)))
df <- temp[[1]]$coeff
colnames(df)[1] <- as.character(temp[[1]]$tau)
for (i in 2:length(temp)){
df[,i] <- temp[[i]]$coefficients[,1]
colnames(df)[i] <- as.character(temp[[i]]$tau)
}
t(df)
New version: alter your creation of coeflist:
coeflist <- lapply(listdf, function(x){data.frame(x$coefficients[,1])})
coeflist
That will bring in only the coefficients and omit the upper and lower bounds. Then cbind
them together and use the names of listdf
to clean up.
coef.mtx <- do.call(cbind, coeflist)
colnames(coef.mtx) <- names(listdf)
coef.mtx
精彩评论