0

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
sm925
  • 2,648
  • 1
  • 16
  • 28
Reza
  • 147
  • 7
  • 4
    Short answer, yes, it's possible. – Hackerman Jan 03 '18 at 20:44
  • This sounds great. Can you shed some more light? – Reza Jan 03 '18 at 20:52
  • 2
    Did you try anything at all? Where exactly are you getting stuck? We frown on questions that appear to just be "do this for me" requests without asking a specific programming question. – MrFlick Jan 03 '18 at 20:54
  • 1
    Please also be clear on what results you are interested in - if you look at `str(results_y100)` there's a lot there. What exactly do you want to pull out and combine with the others? – Gregor Thomas Jan 03 '18 at 21:00

1 Answers1

0

See documentation of the function boot (e.g.: 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$ (e.g.: 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
kamila
  • 122
  • 1
  • 2
  • 13