【问题标题】:function doesn't work in R ("argument is not numeric or logical")函数在 R 中不起作用(“参数不是数字或逻辑”)
【发布时间】:2017-10-10 21:53:44
【问题描述】:

我想在 R 中执行来自以下教科书的函数(在第 20 页,但我在下面发布):media.readthedocs.org/pdf/little-book-of-r-for-multivariate- analysis/latest/little-book-of-r-for-multivariate-analysis.pdf

我正在尝试的数据集(本 PDF 中使用的数据集)可以在这里找到:

wine <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data",
                   sep=",")

函数先定义如下,然后执行(最后一行):

calcBetweenGroupsVariance <- function(variable,groupvariable)
{
# find out how many values the group variable can take
groupvariable2 <- as.factor(groupvariable[[1]])
levels <- levels(groupvariable2)
numlevels <- length(levels)
# calculate the overall grand mean:
grandmean <- mean(variable)
# get the mean and standard deviation for each group:
numtotal <- 0
denomtotal <- 0
for (i in 1:numlevels)
{
leveli <- levels[i]
levelidata <- variable[groupvariable==leveli,]
levelilength <- length(levelidata)
# get the mean and standard deviation for group i:
meani <- mean(levelidata)
sdi <- sd(levelidata)
numi <- levelilength * ((meani - grandmean)^2)
denomi <- levelilength
numtotal <- numtotal + numi
denomtotal <- denomtotal + denomi
}
# calculate the between-groups variance
Vb <- numtotal / (numlevels - 1)
Vb <- Vb[[1]]
return(Vb)
}
calcBetweenGroupsVariance (wine[2],wine[1])

它应该根据三个标签(第一列)给我变量“V2”(第二列)的组间方差。不幸的是,R 告诉我:

数据集的结构如下所示:

我不知道如何解决这个问题。根据 str(),第二列包含数字数据。我也在另一个具有相同问题的数据集上尝试了此功能。我搜索了这个错误消息,有很多基于它的主题,但我无法与我的问题建立任何类比。

如果有人可以提示我该怎么做,我将非常感激!如果您需要更多信息,请告诉我。

非常感谢,

【问题讨论】:

  • 使用wine[1]wine[2] 可能不是你想要的。试试wine[[1]]wine[[2]]
  • 也应该是groupvariable2 &lt;- as.factor(groupvariable)levelidata &lt;- variable[groupvariable==leveli]
  • @MrFlick:我遵循了您的 3 条建议,现在效果很好。非常感谢您的回答!

标签: r function


【解决方案1】:

看起来这本书的作者在如何将参数传递给函数方面做出了一些不寻常的决定。在这种情况下,如果您传入数据向量而不是要求用户传入整个 data.frame,则更有意义(并且通常更有用)。所以这里对函数本身和它的调用方式进行了更改,应该让它运行。

calcBetweenGroupsVariance <- function(variable, groupvariable) {
  # find out how many values the group variable can take
  groupvariable2 <- as.factor(groupvariable)
  levels <- levels(groupvariable2)
  numlevels <- length(levels)
  # calculate the overall grand mean:
  grandmean <- mean(variable)
  # get the mean and standard deviation for each group:
  numtotal <- 0
  denomtotal <- 0
  for (i in 1:numlevels)
  {
    leveli <- levels[i]
    levelidata <- variable[groupvariable==leveli]
    levelilength <- length(levelidata)
    # get the mean and standard deviation for group i:
    meani <- mean(levelidata)
    sdi <- sd(levelidata)
    numi <- levelilength * ((meani - grandmean)^2)
    denomi <- levelilength
    numtotal <- numtotal + numi
    denomtotal <- denomtotal + denomi
  }
  # calculate the between-groups variance
  Vb <- numtotal / (numlevels - 1)
  Vb <- Vb[[1]]
  return(Vb)
}

然后调用它

calcBetweenGroupsVariance (wine[[2]], wine[[1]])
# or 
calcBetweenGroupsVariance (wine$V2, wine$V1)

【讨论】:

    【解决方案2】:

    尝试将na.rm = TRUE 添加到您的grandmean &lt;- mean(variable)

    【讨论】:

      猜你喜欢
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      相关资源
      最近更新 更多