2017-09-15 2 views
1

Je sais comment bootstrap la moyenne d'un vecteur:bootstrap moyenne pondérée en R

library(boot) 
samplemean <- function(x, d) { 
    return(mean(x[d])) 
} 
results_qsec <- boot(data=mtcars$qsec, statistic = samplemean, R=1000) 

mais comment puis-je amorcer la moyenne pondérée, compte tenu des valeurs d'instance sont en mtcars$qsec et poids sur ces valeurs sont en mtcars$wt ?

Répondre

1

L'astuce consiste à spécifier les poids pour weighted.mean dans le cadre de l'argument ... à boot. Ici j'utilise j pour les poids, et le passe en tant que trame de données, pour correspondre à l'argument data =.

Ici, vous allez:

samplewmean <- function(d, i, j) { 
    d <- d[i, ] 
    w <- j[i, ] 
    return(weighted.mean(d, w)) 
    } 

results_qsec <- boot(data= mtcars[, 7, drop = FALSE], 
        statistic = samplewmean, 
        R=10000, 
        j = mtcars[, 6 , drop = FALSE]) 

retours:

ORDINARY NONPARAMETRIC BOOTSTRAP 


Call: 
boot(data = mtcars[, 7, drop = FALSE], statistic = samplewmean, 
    R = 10000, j = mtcars[, 6, drop = FALSE]) 


Bootstrap Statistics : 
    original  bias std. error 
t1* 17.75677 0.0006948823 0.3046888 

Comparer avec:

weighted.mean(mtcars[,7], mtcars[,6]) 
[1] 17.75677 
+1

haha vient de voir ça. Merci :) –

1

, voici comment:

samplewmean <- function(data, d) { 
    return(weighted.mean(x=data[d,1], w=data[d,2])) 
} 

results_qsec <- boot(data=mtcars[,c(7,6)], statistic = samplewmean, R=1000)