简体   繁体   中英

How to combine the outputs in a single data frame in r?

I have three outputs from three different bootstrap groups in R.

The results of Bootstrap Statistics are generated as original, bias and std. error values for each group.

Is it possible to define a function to put all the results together in a single data frame?

y <- rgamma(30,1,1) + rnorm(30,0,0.01)
y60 <- rgamma(60,1,1) + rnorm(60,0,0.01)
y100 <- rgamma(100,1,1) + rnorm(100,0,0.01)
minusL <- function(params, data) {
-sum(log(dgamma(data, params[1], params[2])))
}
fit <- nlm(minusL, c(1,1), data=y)
fit
gammamedian<-function(data) {
fit <- nlm(minusL, c(1,1), data=data)
qgamma(.5, fit$estimate[1], fit$estimate[2])
}
gengamma <- function(data, params){
rgamma(length(data), params[1], params[2])}
library(boot)
results_y <- boot(y, gammamedian, R=100, sim="parametric", ran.gen=gengamma,    
mle=fit$estimate)
results_y
results_y60 <- boot(y60, gammamedian, R=100, sim="parametric",   
ran.gen=gengamma, mle=fit$estimate)
results_y60
results_y100 <- boot(y100, gammamedian, R=100, sim="parametric",   
ran.gen=gengamma, mle=fit$estimate)
results_y100

See documentation of the function boot (eg: help("boot") ). You can find names an descriptions of all output values of the function there in the section called "Value". You can access them using [] or $ (eg: results_y100$t0 or results_y100["t0"] ). You can select ones you are interested in and combine them into a data frame, for example like this:

Put all outputs of interest to a list:

my_list <- list(results_y = boot(y, gammamedian, R=100, sim="parametric", ran.gen=gengamma, mle=fit$estimate),
results_y60 = boot(y60, gammamedian, R=100, sim="parametric",ran.gen=gengamma, mle=fit$estimate),
results_y100 = boot(y100, gammamedian, R=100, sim="parametric", ran.gen=gengamma, mle=fit$estimate))

Define function which extracts the value you are interested in from all list elements:

get_val <- function(val) unlist(sapply(my_list, function(X) X[val]))

Create a data frame using this function:

my_df <- data.frame(t0 = get_val("t0"),
            R = get_val("R"))
rownames(my_df) <- names(my_list)
my_df

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM