Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to formula class #1

Closed
zx8754 opened this issue Jul 27, 2022 · 2 comments
Closed

Convert to formula class #1

zx8754 opened this issue Jul 27, 2022 · 2 comments

Comments

@zx8754
Copy link

zx8754 commented Jul 27, 2022

Related SO post - https://stackoverflow.com/q/73136299/680068

Where lm works fine and clusWilcox.test fails. lm manual says:

formula - an object of class "formula" (or one that can be coerced to that class)

In clusWilcox.test check if it is not class of formula, then coerce to formula to avoid the error.

@wenjie2wang
Copy link
Owner

Thanks. It looks like a good suggestion that can be easily applied.

@wenjie2wang
Copy link
Owner

In brief, this "issue" should be resolved by bd61027.

To be more specific, the clusWilcox.test() is an S3 generic function, with an S3 method for formula objects. The default method will be called if the input x is not a formula object. The substitute(mpg ~ i, list(i = as.name(x)) produces a language object, and thus, the default method is called, which (of course) does not know how to handle the input properly. I do not think it is an issue of the function. But why does lm() work? Notice that lm() is not a generic function. The conversion from language to formula is actually done in model.frame.default().

remotes::install_github("wenjie2wang/clusrank")
library(clusrank)
packageVersion("clusrank")
#> [1] '1.0.3'

## example data
df <- mtcars[, c("vs", "am", "mpg")]
names(df)
#> [1] "vs"  "am"  "mpg"
# [1] "vs"  "am"  "mpg"

varlist <- names(df)[1:2]
# [1] "vs" "am"

lapply(varlist, function(x) {
  clusWilcox.test(substitute(mpg ~ i, list(i = as.name(x))), data = df)
})
#> [[1]]
#> 
#>  Clustered Wilcoxon rank sum test using Rosner-Glynn-Lee method
#> 
#> data:  mpg; group: vs; (from df)
#> number of observations: 32;  number of clusters: 32
#> Z = 3.9342, p-value = 8.349e-05
#> alternative hypothesis: true difference in locations is not equal to 0
#> 
#> 
#> [[2]]
#> 
#>  Clustered Wilcoxon rank sum test using Rosner-Glynn-Lee method
#> 
#> data:  mpg; group: am; (from df)
#> number of observations: 32;  number of clusters: 32
#> Z = 3.1291, p-value = 0.001753
#> alternative hypothesis: true difference in locations is not equal to 0

Created on 2022-07-28 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants