在R中将Levene检验和双向方差分析放入用户定义函数中。

huangapple go评论70阅读模式
英文:

Putting a levene test and two-way ANOVA into a user-define function in R

问题

我一直在尝试创建一个自定义/用户定义的函数来执行Levene检验和双因素方差分析。我在函数的{参数}中努力使公式因子唯一。在查看了这篇帖子之后:
https://stackoverflow.com/questions/75027266/error-custom-function-that-has-tilde-and-or-dollar-sign-in-the-body-in
我创建了以下内容。但是,它不起作用。

test_levene <- function(df, contvar, catvar, catvar2){
  contvar <- as.character(substitute(contvar))
  catvar <- as.character(substitute(catvar))
  catvar2 <- as.character(substitute(catvar2))
  fmla <- as.formula(contvar ~ catvar*catvar2)
  require(car)
  levene <- leveneTest(fmla,data=df)
  pvalue <- levene[[3]][1]
}
#产生错误:Error in leveneTest.default(y = y, group = group, ...) : 
  y is not a numeric variable

上述提到的帖子使用了reformulate,但这会不断出现问题/错误。使用reformulate与使用as.factor相比有什么好处?这两者之间有什么区别。

类似地,对于双因素方差分析,我想要的结构如下。如何使其工作!

test_twoway <- function(df, contvar, catvar, catvar2) {
     twoway <- aov(contvar ~ catvar * catvar2, data = df)
     twoway <- summary(twoway)
     ph1 <- TukeyHSD(twoway, catvar)
     ph2 <- TukeyHSD(twoway, catvar2)
}
英文:

I have been trying to make a custom/user-defined function to perform a levene test and a two-way ANOVA. I am struggling to make the formula factors unique in the {aruguments} in the function. After looking at this post:
https://stackoverflow.com/questions/75027266/error-custom-function-that-has-tilde-and-or-dollar-sign-in-the-body-in
I have created the following. However, it is not working.

test_levene &lt;- function(df, contvar, catvar, catvar2){
  contvar &lt;- as.character(substitute(contvar))
  catvar &lt;- as.character(substitute(catvar))
  catvar2 &lt;- as.character(substitute(catvar2))
  fmla &lt;- as.formula(contvar ~ catvar*catvar2)
  require(car)
levene &lt;- leveneTest(fmla,data=df)
pvalue &lt;- levene[[3]][1]
}
#Produces the error: Error in leveneTest.default(y = y, group = group, ...) : 
  y is not a numeric variable

The mentioned post uses reformulate but that keeps making problems/errors. Is there a benefit to using reformulate over as.factor? What is the difference between the two.

Similarly with the two-way ANOVA, this is the structure I would like. How can I make this work!

test_twoway &lt;- function(df, contvar, catvar, catvar2) {
     twoway &lt;- aov(contvar ~ catvar * catvar2, data = df)
     twoway &lt;- summary(twoway)
     ph1 &lt;- TukeyHSD(twoway, catvar)
     ph2 &lt;- TukeyHSD(twoway, catvar2)
}

答案1

得分: 1

一种可能不符合惯用方法的方式:

test_levene <- function(df, contvar, catvar, catvar2){
contvar = as.character(substitute(contvar))
catvar = as.character(substitute(catvar))
catvar2 = as.character(substitute(catvar2))
fmla = as.formula(paste(contvar, "~", catvar, "*", catvar2))
test_result = leveneTest(fmla, data = df)

对测试结果进行一些操作

test_result
}

(请注意使用了 `paste`)

iris$Color <- gl(3, 50, labels = c('蓝色', '紫色', '深蓝色'))
test_levene(df = iris, Sepal.Length, Species, Color)
Levene方差齐性检验(中心 = 中位数)
自由度 F 值 Pr(>F)
组别 2 6.3527 0.002259 **
147


显著性代码: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1


<details>
<summary>英文:</summary>

One approach, perhaps not idiomatic:

test_levene <- function(df, contvar, catvar, catvar2){
contvar = as.character(substitute(contvar))
catvar = as.character(substitute(catvar))
catvar2 = as.character(substitute(catvar2))
fmla = as.formula(paste(contvar, "~", catvar, "*", catvar2))
test_result = leveneTest(fmla, data = df)

do something with test result

test_result
}

(note the use of `paste`)

> iris$Color <- gl(3, 50, labels = c('blue', 'violet', 'darkblue'))
> test_levene(df = iris, Sepal.Length, Species, Color)
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 2 6.3527 0.002259 **
147

Signif. codes: 0 '' 0.001 '' 0.01 '' 0.05 '.' 0.1 ' ' 1


</details>



huangapple
  • 本文由 发表于 2023年1月9日 16:36:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定