Reputation: 55
I am using R for my data analysis. I want to find the quantiles / p-value for bootstrapped correlation of two numeric vectors. After calculating the correlation (method = kendall) of the two vectors I bootstrapped the function. Somehow the function for finding the quantiles doesn't work
I am using the #mosaic package and tried the quantile function below. The correlation test for the two vectors and the bootstrapping worked well, but the both quantile functions I tried do not work. Please see my code below:
#Score and Population are the vectors (both numeric), Index 2019 is the dataset I am analysing
##Correlation Kendall
cor.test(Index2019$Score, Index2019$Population, method="kendall")
##Bootstraping
set.seed(1896)
H1_Bootstrapping <- do(1000) * cor.test(Score~Population, data= resample(Index2019), method="kendall")
#QUANTILE FUNCTIONS I tried
quantile( ~ cor.test, data = H1_Bootstrapping, probs = c(0.025, 0.975))
qdata(~cor.test,probs=c(.05,.95),data = H1_Bootstrapping)
quantile(H1_Bootstrapping, probs=c(0.05,0.95))
quantile(H1_Bootstrapping, probs=c(0.05,0.95),na.rm=TRUE)
I expect the values for 0.05 and 0.95 confidence interval but actually getting following errors when trying above functions:
quantile( ~ cor.test, data = H1_Bootstrapping, probs = c(0.025, 0.975))
ERROR MESSAGE:
Error in quantile.default(eval(formula[[2]], data, .envir), ...) :
anyNA() applied to non-(list or vector) of type 'closure'
qdata(~cor.test,probs=c(.05,.95),data = H1_Bootstrapping)
ERROR MESSAGE:
Error in quantile.default(x, ..., na.rm = na.rm) :
formal argument "probs" matched by multiple actual arguments
quantile(H1_Bootstrapping, probs=c(0.05,0.95))
ERROR MESSAGE:
Error in quantile.default(x, ..., na.rm = na.rm) :
missing values and NaN's not allowed if 'na.rm' is FALSE
quantile(H1_Bootstrapping, probs=c(0.05,0.95),na.rm=TRUE)
ERROR MESSAGE:
Error in (1 - h) * qs[i] : non-numeric argument to binary operator
Can you help me for any of the functions? Many thanks for your help!
Upvotes: 2
Views: 277
Reputation: 24262
In H1_Bootstrapping$z
you can find the bootstrap values of the Kendall correlation.
library(mosaic)
## Generate data
set.seed(12345)
n <- 100
Index2019 <- data.frame(Score=rnorm(n), Population=rnorm(n))
## Correlation Kendall
cor.test(Index2019$Score, Index2019$Population, method="kendall")
## Bootstraping
set.seed(1896)
H1_Bootstrapping <- do(1000) * cor.test(Score~Population, data= resample(Index2019),
method="kendall")
## QUANTILE FUNCTIONS
quantile( ~ z, data = H1_Bootstrapping, probs = c(0.05, 0.95))
# 5% 95%
# -0.6468391 2.4376336
qdata(~z, p=c(.05,.95), data = H1_Bootstrapping)
# quantile p
# 5% -0.6468391 0.05
# 95% 2.4376336 0.95
quantile(H1_Bootstrapping$z, probs=c(0.05,0.95))
# 5% 95%
# -0.6468391 2.4376336
Upvotes: 1