【问题标题】:Issue with do.call specification of second argument第二个参数的 do.call 规范存在问题
【发布时间】:2020-07-30 18:44:10
【问题描述】:

我创建了一个函数来评估两个统计测试的性能。在这个函数中,我使用第一个函数中的调用函数来创建我的数据生成。 (已经对代码量感到抱歉)感谢您的帮助! 问题出现在第一个 for 循环中的第一个 if else 语句中。错误说: [.data.frame(Design, RowDesign, ) 中的错误: 找不到对象“RowDesign” 这是函数:

SimStudy <- function(Design, RowDesign, K){
  #Design = Design Matrix
  #RowDesign = Number that indicates which row of Design Matrix is used
  #K = number that indicates amount of generated data sets for each row
  Results <- matrix(NA, nrow=K, ncol=4)
  for(k in 1:K){
    if(normal) {
      SimuDat <- call("DataGeneration1", x=Design[RowDesign, ])
    }else{
      SimuDat <- call("DataGeneration2", x=Design[RowDesign, ])
    }
    #Analysis with both methods
    #Analysis with Welchs´s T-test
    Analysis_old <- Method_old(SimData=SimuDat)
    #Analysis with Trimmed F-test
    Analysis_new <- Method_new(SimData=SimuDat)
    P_old <- Analysis_old$p.value
    P_new <- Analysis_new$p.value
    #store in Vector (optional)
    ResultsAnalysis <- c(P_old, P_new)
    #Evaluation
    Hyp <- ifelse(Design[RowDesign,"ef"] == 0, TRUE, FALSE)
    TypeIerror_old <- 0
    TypeIerror_new <- 0
    TypeIIerror_old <- 0
    TypeIIerror_new <- 0
    if(Hyp==TRUE) {#i do not need to write ==TRUE necessarily
      #Type I error
      if(P_old < 0.05){
        TypeIerror_old <- TypeIerror_old + 1
      }
      if(P_new < 0.05){
        TypeIerror_new <- TypeIerror_new + 1
      }
      #Type II error
      if(P_old >= 0.05){
        TypeIIerror_old <- TypeIIerror_old + 1
      }
      if(P_new >= 0.05){
        TypeIIerror_new <- TypeIIerror_new + 1
      }
    }#end if hyp
    Results[k, ] <- c(TypeIerror_old, TypeIerror_new, TypeIIerror_old, TypeIIerror_new)
  }#end for 1
  return(Results)
}

这是我调用函数的代码部分:

totalcells <- nrow(Design1)
magic_for(put,silent=TRUE)
for(i in 1:totalcells){
  RowDesign <- i
  MyResult <- SimStudy(Design=Design1, RowDesign=RowDesign, K=4) 
}#end loop Sim All rows
Result <- magic_result()

最后,运行代码所需的函数和包:

#Packages necessary
install.packages("stats") 
install.packages("PearsonDS")
install.packages("dplyr")
install.packages("magicfor")
library(stats)
library(PearsonDS)
library(dplyr)
library(magicfor)
#Preperation to creat fulfactorial Design Matrix
samp1 <- c(1,2,3,4)
samp2 <- c(1,2,3,4)
ef <- c(0,0.2,0.5,0.8)
vari1 <- c(3)
vari2 <- c(3,9)
Design1 <- expand.grid(samp1=samp1, samp2=samp2, ef=ef, vari1=vari1, vari2=vari2)
#Functions necessary
DataGeneration1 <- function(samp1, samp2, ef, vari1, vari2){
  gen1 <- rnorm(n=samp1, mean=1, sd=sqrt(vari1))
  gen2 <- rnorm(n=samp2, mean=1+ef, sd=sqrt(vari2))
  Y <- c(gen1, gen2)
  group <- as.factor(c(rep(1, times=length(gen1)), rep(2, times=length(gen2))))
  SimData <- data.frame(Y,group)
  return(SimData)
}
DataGeneration2 <- function(samp1, samp2, ef, vari1, vari2){
  gen1 <- rnorm(n=samp1, mean=1, sd=sqrt(vari1)+2)
  gen2 <- rnorm(n=samp2, mean=1+ef, sd=sqrt(vari2)+1)
  Y <- c(gen1, gen2)
  group <- as.factor(c(rep(1, times=length(gen1)), rep(2, times=length(gen2))))
  SimData <- data.frame(Y,group)
  return(SimData)
}
Method_old<- function(SimData){
  formula <- Y~group
  res <- t.test(formula, data = SimData)
  return <- res
}
Method_new<- function(SimData){
  formula <- Y~group
  res <- wilcox.test(formula, data = SimData)
  return <- res
}

【问题讨论】:

  • 请将您的数据分享给make your code reproducibledput() 是一个很好的功能,可以轻松包含数据
  • 什么是全阶乘矩阵?你能详细说明一下吗? do.call 仅适用于列表
  • @JanBoyer,我拿了你的小费并添加了我的脚本。也许这样更容易理解我的问题

标签: r dataframe format arguments do.call


【解决方案1】:

也许你可以试试

if(normal){
      SimData <- do.call(MyDataGeneration1, as.list(x[y, ]))
    }else{
      SimData <- do.call(MyDataGeneration2, as.list(x[y, ]))
    }

【讨论】:

  • 感谢您的提示!我试过了,不幸的是它仍然给我同样的错误。
猜你喜欢
  • 2011-07-31
  • 1970-01-01
  • 2021-12-13
  • 2011-11-09
  • 1970-01-01
  • 2014-12-27
  • 2016-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多