简体   繁体   English

如何在r的单个数据帧中组合输出?

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

I have three outputs from three different bootstrap groups in R. 我有R中三个不同引导程序组的三个输出。

The results of Bootstrap Statistics are generated as original, bias and std. Bootstrap Statistics的结果生成为原始,bias和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") ). 请参阅功能boot文档(例如: 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"] ). 您可以使用[]$访问它们(例如: results_y100$t0results_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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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