Search code examples
rpsych

R psych, cohen.d error Error in `.rowNamesDF<-`(x, value = value) : invalid 'row.names' length


I get the below error when trying to compute effect size using cohen.d function in psych. Please see example data below.

library(psych)
x <- c(1, 2, 3, 4, 5, 6, 7, 8)
y <- c(1, 1, 1, 1, 2, 2, 2, 2)
xy <- data.frame(x,y)
names(xy) <- c("x", "group")
CohensD <- cohen.d(xy, "group", alpha=.05)

Error in .rowNamesDF<-(x, value = value) : invalid 'row.names' length


Solution

  • Probably there is a bug as it works fine in 1.8.12, also it generates the same error with

    cohen.d(sat.act[1:8, c('education','gender')], "gender")
    

    but not with

    cohen.d(sat.act[1:8, c('education','gender','age')], "gender")
    

    So, should work fine with a data frame has more than two columns

    x <- c(1, 2, 3, 4, 5, 6, 7, 8)
    y <- c(1, 1, 1, 1, 2, 2, 2, 2)
    xy <- data.frame(x=x, group=y, z=rnorm(8))
    cohen.d(xy, "group")
    
    Call: cohen.d(x = xy, group = "group")
    Cohen d statistic of difference between two means
      lower effect upper
    x  0.53   3.58  6.60
    z -0.71   0.88  2.37
    
    Multivariate (Mahalanobis) distance between groups
    [1] 3.6
    r equivalent of difference between two means
       x    z 
    0.87 0.40