strengejacke/sjmisc

weights argument not working in frq

LeeFran opened this issue · 11 comments

Hello. The weights argument in frq does not appear to be working.

code: frq(mydata$Country, weights=mydata$weight)
error message: Weights mydata$weight not found in data.

The weight variable does exist in the data.
code: mode(mydata$weight)
result: [1] "numeric"

Not sure if it's relevant here, but the weight.by argument in your xtab function from sjPlot works great! Hoping you can get it working for frequencies as well.

Thanks so much.
ps - this is my very first time on GitHub so please let me know if I should be reporting issues differently.

The weights-argument just needs the variable name, bare or as string:

data(efc)
efc$w <- abs(rnorm(n = nrow(efc), mean = 1, sd = .5))

frq(efc, c160age, auto.grp = 5, weights = w)
frq(efc, c160age, auto.grp = 5, weights = "w")

The issue to me is that we sjPlot users may like the conventional style of frq(efc$c160age, auto.grp = 5, weights = w), or frq(efc$c160age, auto.grp = 5, weights = efc$w). Could we keep this style, please? Because frq(efc$c160age) remains working, I hope we don't need to switch to the all new syntax of frq(data, var) just because of we want to use a weight. We are ready to switch from table() to frq(). Please consider this serious request. Thanks!

No sorry Daniel. It still isn't working. I stripped down the code a bit and looked at a simple categorical variable in the efc dataset. I ran it four ways. The first 2 methods produce unweighted frequencies of the gender variable (as they should). The second 2 methods also produce unweighted frequencies of the gender variable (they should be weighted) plus they also run a frequency of the weight variable itself. Can you please try this code and let me know if I'm doing something wrong here?
data(efc)
efc$w <- abs(rnorm(n = nrow(efc), mean = 1, sd = .5))
frq(efc, e16sex)
frq(efc, e16sex, weights = NULL)
frq(efc, e16sex, weights = w)
frq(efc, e16sex, weights = "w")

@LeeFran Do you have all packages up to date? For me, everything works like intended:

library(sjmisc)
#> Install package "strengejacke" from GitHub (`devtools::install_github("strengejacke/strengejacke")`) to load all sj-packages at once!
data(efc)
efc$w <- abs(rnorm(n = nrow(efc), mean = 1, sd = .5))

frq(efc, e16sex)
#> 
#> # elder's gender (e16sex) <numeric> 
#> # total N=908  valid N=901  mean=1.67  sd=0.47
#>  
#>  val  label frq raw.prc valid.prc cum.prc
#>    1   male 296   32.60     32.85   32.85
#>    2 female 605   66.63     67.15  100.00
#>   NA     NA   7    0.77        NA      NA

frq(efc, e16sex, weights = NULL)
#> Weights `NULL` not found in data.
#> 
#> # elder's gender (e16sex) <numeric> 
#> # total N=908  valid N=901  mean=1.67  sd=0.47
#>  
#>  val  label frq raw.prc valid.prc cum.prc
#>    1   male 296   32.60     32.85   32.85
#>    2 female 605   66.63     67.15  100.00
#>   NA     NA   7    0.77        NA      NA

frq(efc, e16sex, weights = w)
#> 
#> # elder's gender (e16sex) <numeric> 
#> # total N=918  valid N=918  mean=1.66  sd=0.47
#>  
#>  val  label frq raw.prc valid.prc cum.prc
#>    1   male 314    34.2      34.2    34.2
#>    2 female 604    65.8      65.8   100.0
#>   NA     NA   0     0.0        NA      NA

frq(efc, e16sex, weights = "w")
#> 
#> # elder's gender (e16sex) <numeric> 
#> # total N=918  valid N=918  mean=1.66  sd=0.47
#>  
#>  val  label frq raw.prc valid.prc cum.prc
#>    1   male 314    34.2      34.2    34.2
#>    2 female 604    65.8      65.8   100.0
#>   NA     NA   0     0.0        NA      NA

Created on 2018-12-11 by the reprex package (v0.2.1)

@frankcsliu I have revised the function, so you can also use these options:

library(sjmisc)
data(efc)
efc$w <- abs(rnorm(n = nrow(efc), mean = 1, sd = .5))

frq(efc, e16sex)
#> 
#> # elder's gender (e16sex) <numeric> 
#> # total N=908  valid N=901  mean=1.67  sd=0.47
#>  
#>  val  label frq raw.prc valid.prc cum.prc
#>    1   male 296   32.60     32.85   32.85
#>    2 female 605   66.63     67.15  100.00
#>   NA     NA   7    0.77        NA      NA

frq(efc$e16sex)
#> 
#> # elder's gender (x) <numeric> 
#> # total N=908  valid N=901  mean=1.67  sd=0.47
#>  
#>  val  label frq raw.prc valid.prc cum.prc
#>    1   male 296   32.60     32.85   32.85
#>    2 female 605   66.63     67.15  100.00
#>   NA     NA   7    0.77        NA      NA

frq(efc, e16sex, weights = w)
#> 
#> # elder's gender (e16sex) <numeric> 
#> # total N=921  valid N=921  mean=1.67  sd=0.47
#>  
#>  val  label frq raw.prc valid.prc cum.prc
#>    1   male 304   33.01     33.01   33.01
#>    2 female 617   66.99     66.99  100.00
#>   NA     NA   0    0.00        NA      NA

frq(efc, e16sex, weights = "w")
#> 
#> # elder's gender (e16sex) <numeric> 
#> # total N=921  valid N=921  mean=1.67  sd=0.47
#>  
#>  val  label frq raw.prc valid.prc cum.prc
#>    1   male 304   33.01     33.01   33.01
#>    2 female 617   66.99     66.99  100.00
#>   NA     NA   0    0.00        NA      NA

frq(efc, e16sex, weights = efc$w)
#> 
#> # elder's gender (e16sex) <numeric> 
#> # total N=921  valid N=921  mean=1.67  sd=0.47
#>  
#>  val  label frq raw.prc valid.prc cum.prc
#>    1   male 304   33.01     33.01   33.01
#>    2 female 617   66.99     66.99  100.00
#>   NA     NA   0    0.00        NA      NA

frq(efc$e16sex, weights = efc$w)
#> 
#> # elder's gender (xw) <numeric> 
#> # total N=921  valid N=921  mean=1.67  sd=0.47
#>  
#>  val  label frq raw.prc valid.prc cum.prc
#>    1   male 304   33.01     33.01   33.01
#>    2 female 617   66.99     66.99  100.00
#>   NA     NA   0    0.00        NA      NA

Created on 2018-12-11 by the reprex package (v0.2.1)

Thank you so much!

@LeeFran Did you have the time to check if the latest GitHub-version(s) solve your issue?

@stengejacke Not yet but it is very much on my to-do list. Sorry for the delay. I'm working on this in my free time. Thanks for your patience.

sjmisc 2.7.8 has just been released on CRAN (source pkg available, binaries following). Could you please check if this issue is resolved or not?

@strengejacke Thank you for your patience and hard work. The new version is working properly.

Thanks for confirming that this issue is resolved