Creating Bootstrapped Means and CI from matrix

依然范特西╮ 提交于 2019-12-12 01:45:22

问题


I have the following matrix (let's call it df), for which I would like to create bootstrapped means and 95% confidence intervals for each column, due to the heavily 0 weighted distribution. I would like the mean and CI's to be added to the bottom of the matrix as new rows. This is a small subset of the data, the true data has >600 rows which will make the bootstrapping much more effective.

row.names   V183    V184    V185    V186    V187    V188    V189    V190    V191    V192    V193    V194    V195    V196    V197    V198    V199    V200    V201    V202    V203    V204    V205
1   0.07142857  0.07142857  0.07142857  0.07142857  0.07142857  0.07142857  0.07142857  0.07142857  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  NA  NA
2   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
3   0.022   0.022   0.022   0.022   0.022   0.022   0.022   0.022   0.022   0.022   0.022   0.022   0.022   0.022   0.022   0.022   0   NA  NA  NA  NA  NA  NA
4   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308  0.07692308
5   0   0   0   0   0.066   0.066   0.066   0.066   0.066   0.066   0.066   0.066   0.066   0.066   0   0   0   0   0   0   0   0   0
6   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0.077   0   0   0   0
7   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
8   0.07142857  0.07142857  0.07142857  0.07142857  0.07142857  0.07142857  0.07142857  0.07142857  0.07142857  0.07142857  0.07142857  0   0   0   0   0   0   0   0   0   0   0   0
9   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   NA  NA  NA  NA  NA  NA
10  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
11  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  0.03225806  0.03225806  0.03225806  0.03225806  0.03225806  0.03225806  0.03225806  0.03225806  0.03225806  0.03225806  0.03225806
12  0   0   0   0   0   0   0   0   0   0   0   0   0   NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
13  0   0   0   0   0   0   0   0   0   NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA  NA
14  0   0   0.033   0.033   0.033   0.033   0.033   0.033   0.033   0.033   0.033   0.033   0.033   0.033   0.033   0.033   0.033   0   0   0   0   0   0

I have tried this:

 boot.mean <- function(df,i){boot.mean <- mean(df[i])}
 df["BootMean" ,] <- boot(df, boot.mean, R = 2000)

But it says "undefined columns are selected

So I tried this:

 boot.mean <- function(df[1:23],i){boot.mean <- mean(df[i])}
 df["BootMean" ,] <- boot(df, boot.mean, R = 2000)

But it says there is a "[" that it doesn't like.

I recently tried this:

 n<-length(df)
 B<-1000
 boot.mean <- function(df,i){boot.mean <- mean(df[,i],na.rm = TRUE)}
 df["BootMean" ,] <-for (i in 1:n) {
 boot(df[1:14,i],boot.mean,R=B)
 }

But I receive a "error in evaluating the argument 'x' in selecting a method for function 'mean': Error in df[, i] : incorrect number of dimensions"

Do I need to use an apply function or something??? Please Help, the brain is hurting over this trivial problem!

*****I've made some progress, but am not all the way yet.

I've been able to get a booted mean for a single row by subsetting it out, but I am unable to incorporate a na.rm=T function into the formula, so I also have to manually remove those. Can anyone suggest a way to add the na.rm fn?

df<-subset(dfboot,F_BS_sub[1:323, 1]>=0)
dfa<-df[,1]
dfb<-subset(dfa,V183>=0)
boot.mean <- function(dfb, d) {
  E=dfb[d,]
  return(mean(E))}
b = boot(dfb, boot.mean, R=1000)
b

来源:https://stackoverflow.com/questions/24598896/creating-bootstrapped-means-and-ci-from-matrix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!