R 排列检验计算观察到的t分数

R 排列检验计算观察到的t分数,r,R,我想写一个函数来执行排列测试,但却在努力计算观察到的t分数和排列分数,以及绘制经验分布 变量(y)中出错:对因子x调用变量(x)已失效。使用类似“all(复制的(x)[-1L])”的方法测试常量向量 permutation.test <- function ( gene, # gene pheno, # (binary) phenotype, must be a factor nperm, # number of pe

我想写一个函数来执行排列测试,但却在努力计算观察到的t分数和排列分数,以及绘制经验分布

变量(y)中出错:对因子x调用变量(x)已失效。使用类似“all(复制的(x)[-1L])”的方法测试常量向量

permutation.test <- function

(
    gene,         # gene 
    pheno,        # (binary) phenotype, must be a factor
    nperm,        # number of permutation iterations
    alternative=c("two.sided","less","greater"),
    smooth=0,     # smoothing factor to avoid pval=0
    seed=NULL,    # random seed (for reproducible results)
    do.plot=FALSE # plot the empirical distribution of permuted t-scores
)
{
    ## compute the _observed_ t-score
    obs <- t.test(gene~pheno,var.equal=TRUE)["statistic"]

    ## compute the _permuted_ scores
    perm  <- replicate(1000, t.test(gene, pheno))["statistic"]
    
    ## compute the empirical p-value
    pval <- mean(abs(perm) > abs(obs))/1000
      
    
    if ( do.plot ) {
      hist(perm)
      abline(v=obs, lwd=2, col="purple")
      mean(abs(perm) > abs(obs))
    }
    return(pval)
}

time1 <- system.time(
  p2 <- permutation.test(gene=gene,pheno=as.factor(pheno),nperm=1000,seed=123,do.plot=TRUE)
)
print(time1)

permutation.test查看
gene
pheno
的样子会很有帮助。通常在排列测试中,结果或治疗被随机洗牌,然后计算测试统计量并与观察到的结果进行比较。我在你的代码中没有看到任何基因
或基因的随机乱序。假设“基因”是一个连续变量(考虑到基因作为一种离散实体的通常概念,这似乎不太可能),你需要插入一个
样本
调用来重新排列“基因”或“基因”变量。我想知道这是不是家庭作业?