[R] Cannot calculate confidence intervals NULL

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Wed Nov 15 22:21:40 CET 2023


I believe the problem is here:

cor1 <- cor(x1, y1, method="spearman")
 cor2 <- cor(x2, y2, method="spearman")

The x's and y's are not looked for in data (i.e. NSE) but in the
environment where the function was defined, which is standard evaluation.
Change the above to:

cor1 <- with(d, cor(x1, y1, method="spearman"))
 cor2 <- with(d, cor(x2, y2, method="spearman"))

and all should be fine.

-- Bert

On Wed, Nov 15, 2023 at 12:54 PM varin sacha via R-help <
r-help using r-project.org> wrote:

> R-Experts,
>
> Here below my R code working without error message but I don't get the
> results I am expecting.
> Here is the result I get:
>
> [1] "All values of t are equal to 0.28611928397257 \n Cannot calculate
> confidence intervals"
> NULL
>
> If someone knows how to solve my problem, really appreciate.
> Best,
> S
>
>
> #########################################################
> # Difference in Spearman rho
>
> library(boot)
>
>
> x1=c(4,6,5,7,8,4,2,3,5.5,6.7,5.5,3.5,2,1,3,5,6,3.5,2.5,2,1,2,3,2,1,2,3,4,3,4)
>
>
> y1=c(10,14,12.5,21,15,16,17.5,11,11.5,21,19,16,17.5,18,18.5,12,13,14,11,11,12,18,20,13,23,12,11,14,16,11)
>
> x2=c(5,3,4,2,1,1,1,2,3,4,5,4,3,2,1,3,4.5,4.5,5.5,6,5,4,7,8,3,4,2,5,4,3)
>
>
> y2=c(11,12,13,11,10,19,21,21,13,15,18,13,12,14,19,18.5,17.5,12.5,10,9,11,13,14,16,11,18,14,13,12,12)
>
> # Function to calculate the difference in Spearman coefficients
> pearson_diff <- function(data, indices) {
>
> # Sample the data
>   d <- data[indices, ]
>
>  # Calculate the Spearman correlation coefficients for every sample
>   cor1 <- cor(x1, y1, method="spearman")
>   cor2 <- cor(x2, y2, method="spearman")
>
> # Return the difference
>   return(cor1 - cor2)
> }
>
> # Create a data.frame with the data
> data <- data.frame(x1, y1, x2, y2)
>
> # Use the boot function to apply the bootstrap
> set.seed(123) # For reproducibility
> bootstrap_results <- boot(data = data, statistic = pearson_diff, R = 1000)
>
> # Calculate all the 95% confidence interval
> boot.ci(bootstrap_results, type = "all")
> ###############################################################
>
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list