meff<-function(x, eigen = FALSE, correl = FALSE) { # if the input x is eigenvalues if(eigen) { if(!class(x) %in% c("numeric", "integer")) { stop("Eigenvalues are not numeric or integer.") } k = length(x) evs = x # if the input x is a correlation matrix } else if(correl) { # number of variables k = ncol(x) # dimension checks if(!isSymmetric(x)) { stop("Correlation matrix is not symmetric.") } # convert the correlation matrix to positive-definite require(Matrix) x = as.matrix(nearPD(x)$mat) # get eigenvalues and absolute eigenvalues of correlation matrix evs = eigen(x)$values # otherwise the input x is assumed to be a dataframe A SIMPLE CORRECTION FOR NON-INDEPENDENT TESTS 18 # with variables in columns and observations in rows } else { # number of variables k = ncol(x) # get correlation matrix x = cor(x, use = "pairwise.complete.obs") # convert the correlation matrix to positive-definite. require(Matrix) x = as.matrix(nearPD(x)$mat) # get eigenvalues and absolute eigenvalues of R matrix evs = eigen(x)$values } # effective number of tests (Nyholt, 2004) eff = 1 + (k - 1) * (1 - var(evs) / k) return(eff) } 以上代码如何运行?
时间: 2023-11-13 09:04:14 浏览: 180
这段代码是一个 R 语言的函数,可以用来计算有效测试数。可以将这段代码保存为一个 R 脚本文件,然后在 R 环境中使用 source() 函数读入该文件,然后就可以调用 meff() 函数了。例如:
```
source("path/to/meff.R")
x <- matrix(rnorm(100), ncol = 10)
eff <- meff(x)
```
其中,第一行读入 meff.R 文件,第二行生成一个 10 列的随机矩阵,第三行调用 meff() 函数计算有效测试数。
阅读全文