harrelfe / Hmisc

Harrell Miscellaneous
Other
205 stars 81 forks source link

error calculating with rcorr() function #120

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi,

I'm using the rcorr() example from "Package ‘Hmisc’" from November 7, 2019

if I'm trying the published example, everything ist ok. But if I want to calculate the correlation matrix for columns x and y the, I'm getting an error message. (see below)

x <- c(-2, -1, 0, 1, 2)
y <- c(4, 1, 0, 1, 4)
z <- c(1, 2, 3, 4, NA)
v <- c(1, 2, 3, 4, 5)
rcorr(cbind(x,y))
> Error in matrix(rnorm(n * d), n, d) : invalid 'nrow' value (< 0)

Further issues: I've written an Code in R, with rcorr() to calculate a big correlation matrix. Last time it was working was around May 2019. Recently I tried rerunning the code and I was getting this error message:

Error in rcorr(as.matrix(o_w_CSD_KSS), type = "pearson") : 
  unused argument (type = "pearson")

Another error message relating to rcorr(): I was learning some statistics from another example and got something like this. (dataset is coming from: https://studysites.uk.sagepub.com/dsur/study/articles.htm under chapter 6.

examData <- read.delim("Exam Anxiety.dat", header = TRUE)
# computing p-values of correlation coefficients
examMatrix<-as.matrix(examData[, c("Exam", "Anxiety")])
rcorr(examMatrix)

[20,] -0.001405012  0.15748544 -0.0729578499  0.302848404  0.157681656 -0.099289738 -0.092430213 -0.19978157
[21,] -0.062027634  0.02261721 -0.1519534279  0.180263508  0.132594815 -0.157301698  0.051600195 -0.31588050
[22,]  0.137019938  0.01987143 -0.0815079170  0.062882864 -0.016685165 -0.056887589 -0.075454718 -0.09027757
[23,] -0.074338494 -0.01589323  0.0340466644  0.102513765 -0.144096280 -0.007199578  0.136943954 -0.07506429
[24,] -0.053900968 -0.22150185  0.0725679656  0.081125188  0.523507004  0.099360897 -0.106041345  0.14067532
[25,]  0.201393705 -0.09765226 -0.2067822590 -0.146102846  0.002855684 -0.111788818  0.055571926 -0.05463886
 [ reached getOption("max.print") -- omitted 15 rows ]

> # or
> # rcorr(as.matrix(examData[, c("Exam", "Anxiety", "Revise")]))
Warning message:
In matrix(rnorm(n * d), n, d) :
  data length [206] is not a sub-multiple or multiple of the number of rows [40]

I'm using:

R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] RColorBrewer_1.1-2 scales_1.0.0       forcats_0.4.0      stringr_1.4.0      dplyr_0.8.0.1     
 [6] purrr_0.3.2        readr_1.3.1        tibble_2.1.1       tidyverse_1.2.1    reshape2_1.4.3    
[11] lubridate_1.7.4    anytime_0.3.3      tidyr_0.8.3        tictoc_1.0         polycor_0.7-10    
[16] ggm_2.3            igraph_1.2.4.1     Hmisc_4.2-0        Formula_1.2-3      survival_2.44-1.1 
[21] lattice_0.20-38    car_3.0-5          carData_3.0-3      ggplot2_3.1.1     
SamGG commented 4 years ago

Hi, I'm using R3.6.1 and I have just installed Hmisc. The reported version is 4.3.0 in my Windows installation. I tried your first example and got a correct answer. I think you should update to the last version of Hmisc and retry from a clean and empty environment. In my test, I don't have that many packages loaded. Best.

library(Hmisc) ; x <- c(-2, -1, 0, 1, 2); y <- c(4, 1, 0, 1, 4); z <- c(1, 2, 3, 4, NA); v <- c(1, 2, 3, 4, 5); rcorr(cbind(x,y)) ; sessionInfo()
#> Loading required package: lattice
#> Loading required package: survival
#> Loading required package: Formula
#> Loading required package: ggplot2
#> 
#> Attaching package: 'Hmisc'
#> The following objects are masked from 'package:base':
#> 
#>     format.pval, units
#>   x y
#> x 1 0
#> y 0 1
#> 
#> n= 5 
#> 
#> 
#> P
#>   x  y 
#> x     1
#> y  1
#> R version 3.6.1 (2019-07-05)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 7 x64 (build 7601) Service Pack 1
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United Kingdom.1252 
#> [2] LC_CTYPE=English_United Kingdom.1252   
#> [3] LC_MONETARY=English_United Kingdom.1252
#> [4] LC_NUMERIC=C                           
#> [5] LC_TIME=English_United Kingdom.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] Hmisc_4.3-0       ggplot2_3.2.1     Formula_1.2-3     survival_2.44-1.1
#> [5] lattice_0.20-38  
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.3          RColorBrewer_1.1-2  pillar_1.4.2       
#>  [4] compiler_3.6.1      highr_0.8           base64enc_0.1-3    
#>  [7] tools_3.6.1         rpart_4.1-15        digest_0.6.22      
#> [10] checkmate_1.9.4     htmlTable_1.13.2    evaluate_0.14      
#> [13] tibble_2.1.3        gtable_0.3.0        pkgconfig_2.0.3    
#> [16] rlang_0.4.1         Matrix_1.2-17       rstudioapi_0.10    
#> [19] yaml_2.2.0          xfun_0.11           gridExtra_2.3      
#> [22] cluster_2.1.0       withr_2.1.2         stringr_1.4.0      
#> [25] dplyr_0.8.3         knitr_1.26          htmlwidgets_1.5.1  
#> [28] nnet_7.3-12         grid_3.6.1          tidyselect_0.2.5   
#> [31] data.table_1.12.6   glue_1.3.1          R6_2.4.1           
#> [34] foreign_0.8-71      rmarkdown_1.17      latticeExtra_0.6-28
#> [37] purrr_0.3.3         magrittr_1.5        backports_1.1.5    
#> [40] scales_1.0.0        htmltools_0.4.0     splines_3.6.1      
#> [43] assertthat_0.2.1    colorspace_1.4-1    stringi_1.4.3      
#> [46] acepack_1.4.1       lazyeval_0.2.2      munsell_0.5.0      
#> [49] crayon_1.3.4

Created on 2019-11-23 by the reprex package (v0.3.0)

ghost commented 4 years ago

Hi SamGG,

Thank you for your fast reply.

Apparently the problem came from different packages I had loaded. Your hint to provide a clean environment helped me.