英文:
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 <- 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]
}
#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 <- function(df, contvar, catvar, catvar2) {
twoway <- aov(contvar ~ catvar * catvar2, data = df)
twoway <- summary(twoway)
ph1 <- TukeyHSD(twoway, catvar)
ph2 <- 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论