错误:在R中,自定义函数的主体中包含了~(波浪号)和/或$(美元符号)。

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

Error: Custom function that has ~ (tilde) and/or $ (dollar sign) in the body in r

问题

Here are the translated parts of your code:

我正在尝试通过在R中使用自定义/用户定义的函数来简化我的代码。

我想将以下内容转化为函数:

```R
shapiro <- shapiro.test(data$count)
if(shapiro$p.value > 0.05) {
  require(tidyverse)
  bart <- bartlett.test(count ~ species, data=data)
} 
if ((shapiro$p.value > 0.05) && exists("bart")){
  if(bart$p.value > 0.05) { 
    OneWay <- aov(count ~ species, data = data) 
    oneway <- summary(OneWay) #normal distribution and equal variance
  } else  {
    welch <- oneway.test(count ~ species, data = data) #Welch ANOVA, normal distribution, unequal variance
  }
} else {
  fligner <- fligner.test(count ~ species, data = data)
   #if not normal distribution, perform fligner-killen homogeneity of variance test
} 

第一组我尝试过的函数:

perform_shapiro <- function(df, contvar) {
   shapiro.test(df$contvar)
}
perform_shapiro(df = data, contvar = count)

其中返回错误:Error in shapiro.test(df$contvar) : is.numeric(x) is not TRUE.

第二组成功了:

perform_barlett <- function(df, contvar, catvar) {
  if(shapiro$p.value > 0.05) {
    require(tidyverse)
    bart <- bartlett.test(contvar ~ catvar, data=df)
    return(bart)
  }
}
perform_bartlett(data, count, species)

但第三组也出现了新错误:

perform_oneway_welch_fligner <- function(df, contvar, catvar){
      if ((shapiro$p.value > 0.05) && exists("bart")){
      if(bart$p.value > 0.05) { 
        OneWay <- aov(contvar ~ catvar, data = df) 
        oneway <- summary(OneWay) #normal distribution and equal variance
        print(oneway)
      } else  {
        welch <- oneway.test(contvar ~ catvar, data = df) #Welch ANOVA, normal distribution, unequal variance
        print(welch)
      }
    } else {
      fligner <- fligner.test(contvar ~ catvar, data = df)
       #if not normal distribution, perform fligner-killen homogeneity of variance test
      print(fligner)
    } 
}
perform_oneway_welch_fligner(data, count, species)

Error in eval(predvars, data, env) : object 'count' not found.

Data

data <- iris
# rename columns
names(data)[names(data) == "Sepal.Length"] <- "count"

FYI shapiro is < 0.05, so bart should not exist, hence fligner should be returned.
Contvar = continuous variable, catvar = categorical variable.

我最初认为错误是由于$和~,但第二个函数效果很好,并且它内部有一个波浪线公式,所以现在我不确定。谢谢!


Please note that I have provided the translated parts of your code as requested. If you have any specific questions or need further assistance with your code, feel free to ask.

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

I am trying to simplify my code by using custom/user-defined functions in r.

I want to turn the following into functions:

shapiro <- shapiro.test(data$count)


if(shapiro$p.value > 0.05) {
require(tidyverse)
bart <- bartlett.test(count ~ species, data=data)
}

if ((shapiro$p.value > 0.05) && exists("bart")){
if(bart$p.value > 0.05) {
OneWay <- aov(count ~ species, data = data)
oneway <- summary(OneWay) #normal distibution and equal varaince
} else {
welch <- oneway.test(count ~ species, data = data) #Welch ANOVA, normal distribution, unequal variance
}
} else {
fligner <- fligner.test(count ~ species, data = data)
#if not normal distribution, perform fligner-killen homogeneity of variance test
}


The first set I have tried:

perform_shapiro <- function(df, contvar) {
shapiro.test(df$contvar)
}
perform_shapiro(df = data, contvar = count)

Which returns the error: Error in shapiro.test(df$contvar) : is.numeric(x) is not TRUE.

The second set is successful with:

perform_barlett <- function(df, contvar, catvar) {
if(shapiro$p.value > 0.05) {
require(tidyverse)
bart <- bartlett.test(contvar ~ catvar, data=df)
return(bart)
}
}
perform_bartlett(data, count, species)

But the third also has a new error:

perform_oneway_welch_fligner <- function(df, contvar, catvar){
if ((shapiro$p.value > 0.05) && exists("bart")){
if(bart$p.value > 0.05) {
OneWay <- aov(contvar ~ catvar, data = df)
oneway <- summary(OneWay) #normal distibution and equal varaince
print(oneway)
} else {
welch <- oneway.test(contvar ~ catvar, data = df) #Welch ANOVA, normal distribution, unequal variance
print(welch)
}
} else {
fligner <- fligner.test(contvar ~ catvar, data = df)
#if not normal distribution, perform fligner-killen homogeneity of variance test
print(fligner)
}
}
perform_oneway_welch_fligner(data, count, species)


Error in eval(predvars, data, env) : object &#39;count&#39; not found.

#### Data

data <- iris

rename columns

names(data)[names(data) == "Sepal.Length"] <- "count"


FYI shapiro is &lt; 0.05, so bart should not exist, hence fligner should be returned. 
Contvar = continuous variable, catvar = categorical variable.

I thought the errors at first were due to $ and ~, but the second function works well and it has a tilde formula inside, so now I am unsure. Thanks!

</details>


# 答案1
**得分**: 1

以下是代码的中文翻译:

``` r
# 执行Shapiro-Wilk正态性检验
perform_shapiro <- function(df, contvar) {
  contvar <- as.character(substitute(contvar))
  shapiro.test(df[[contvar]])
}

# 执行Bartlett方差齐性检验
perform_bartlett <- function(df, contvar, catvar) {
  contvar <- as.character(substitute(contvar))
  catvar <- as.character(substitute(catvar))
  fmla <- reformulate(catvar, contvar)
  shapiro <- shapiro.test(df[[contvar]])
  if (shapiro$p.value > 0.05) {
    bartlett.test(fmla, data = df)
  }
}

# 执行One-Way ANOVA或Welch ANOVA或Fligner-Killeen方差齐性检验
perform_oneway_welch_fligner <- function(df, contvar, catvar){
  contvar <- as.character(substitute(contvar))
  catvar <- as.character(substitute(catvar))
  fmla <- reformulate(catvar, contvar)
  shapiro <- shapiro.test(df[[contvar]])
  bart <- bartlett.test(fmla, data = df)
  if ((shapiro$p.value > 0.05) && !is.null(bart)){
    if(bart$p.value > 0.05) { 
      OneWay <- aov(fmla, data = df) 
      summary(OneWay) # 正态分布和方差齐性
    } else  {
      # Welch ANOVA,正态分布,不等方差
      oneway.test(fmla, data = df) 
    }
  } else {
    # 如果不是正态分布,则执行Fligner-Killeen方差齐性检验
    fligner.test(fmla, data = df)
  } 
}

# 数据集为鸢尾花数据
data <- iris
names(data)[names(data) == "Sepal.Length"] <- "count"

# 执行Shapiro-Wilk正态性检验
perform_shapiro(df = data, contvar = count)
#> 
#>  Shapiro-Wilk正态性检验
#> 
#> 数据:df[[contvar]]
#> W = 0.97609, p-value = 0.01018

# 返回NULL(不可见)
perform_bartlett(data, count, Species)

# 执行One-Way ANOVA或Welch ANOVA或Fligner-Killeen方差齐性检验
perform_oneway_welch_fligner(data, count, Species)
#> 
#>  Fligner-Killeen方差齐性检验
#> 
#> 数据:按Species分类的count
#> Fligner-Killeen:中位数卡方 = 11.618,自由度 = 2,p值 = 0.003

于2023-01-06使用reprex v2.0.2创建

英文:

Here is a full solution, answering to the OP's comments.

perform_shapiro &lt;- function(df, contvar) {
  contvar &lt;- as.character(substitute(contvar))
  shapiro.test(df[[contvar]])
}
perform_bartlett &lt;- function(df, contvar, catvar) {
  contvar &lt;- as.character(substitute(contvar))
  catvar &lt;- as.character(substitute(catvar))
  fmla &lt;- reformulate(catvar, contvar)
  shapiro &lt;- shapiro.test(df[[contvar]])
  if(shapiro$p.value &gt; 0.05) {
    bartlett.test(fmla, data = df)
  }
}
perform_oneway_welch_fligner &lt;- function(df, contvar, catvar){
  contvar &lt;- as.character(substitute(contvar))
  catvar &lt;- as.character(substitute(catvar))
  fmla &lt;- reformulate(catvar, contvar)
  shapiro &lt;- shapiro.test(df[[contvar]])
  bart &lt;- bartlett.test(fmla, data = df)
  if ((shapiro$p.value &gt; 0.05) &amp;&amp; !is.null(bart)){
    if(bart$p.value &gt; 0.05) { 
      OneWay &lt;- aov(fmla, data = df) 
      summary(OneWay) #normal distibution and equal varaince
    } else  {
      # Welch ANOVA, normal distribution, unequal variance
      oneway.test(fmla, data = df) 
    }
  } else {
    #if not normal distribution, perform fligner-killen homogeneity of variance test
    fligner.test(fmla, data = df)
  } 
}

data &lt;- iris
names(data)[names(data) == &quot;Sepal.Length&quot;] &lt;- &quot;count&quot;

perform_shapiro(df = data, contvar = count)
#&gt; 
#&gt;  Shapiro-Wilk normality test
#&gt; 
#&gt; data:  df[[contvar]]
#&gt; W = 0.97609, p-value = 0.01018

# returns NULL invisiby
perform_bartlett(data, count, Species)

perform_oneway_welch_fligner(data, count, Species)
#&gt; 
#&gt;  Fligner-Killeen test of homogeneity of variances
#&gt; 
#&gt; data:  count by Species
#&gt; Fligner-Killeen:med chi-squared = 11.618, df = 2, p-value = 0.003

<sup>Created on 2023-01-06 with reprex v2.0.2</sup>

答案2

得分: 0

第一个函数可以使用匿名函数来解决:

perform_shapiro <- function(x) {
   shapiro.test(x)
}
perform_shapiro(data$count)

(抱歉,在玩弄了一些后,我发现这个方法有效!但在第三个函数中(即用 x 替换公式时)不起作用)

英文:

The first function could be solved using an anonymous function

perform_shapiro &lt;- function(x) {
   shapiro.test(x)
}
perform_shapiro(data$count)

(sorry after playing around a bit I figure out that this works! It did not work in the third function (i.e. replacing the formulas with x))

huangapple
  • 本文由 发表于 2023年1月6日 13:31:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027266.html
匿名

发表评论

匿名网友

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

确定