r-如何使用带有多个参数的mapply复制函数来计算方法的幂



我有独立和依赖的数据集。我想测试因变量和自变量之间的所有可能关系,并最终计算该方法的幂。

# dependent dataset
test_A <- data.frame(matrix(rnorm(100), nr=10, nc=10))
# independent dataset
test_B <- data.frame(matrix(sample(c(0,1,2), 500, replace = TRUE), nr=50, nc=10))
# Find all combination using dependent and independe datasets's variables
A_B_pair <- subset(expand.grid(c1=names(test_A), c2=names(test_B), 
stringsAsFactors = FALSE))
# Main function to estimate the parameter and p-values 
test_function <- function(x,y){
c1 <- test_A [[x]]
c2 <- test_B[[y]]
Data <- data.frame(1, XX=c1, YY=c2)

model_lm <- lm(YY ~ XX, Data)
est_lm <- as.numeric(model_lm$coefficients)[2]
pvalue_lm <- as.numeric(summary(model_lm)$coeffi[,4][2])
return(unlist(data.frame(lm.estimator = est_lm, lm.pvalue =pvalue_lm)))
}
# Using mapply  to get the all pairs estimators and p-values
output <- mapply(test_function, x=A_B_pair$c1, y=A_B_pair$c2)
# transpose the output
output.data <- data.frame(t(output))
# Put all the dependent and independent variables and their estimated values and p-values in a data frame.
output_final <- cbind(A_B_pair, output.data)

我的问题是,我需要将这个函数复制100次,以检查该方法的功效并估计参数。功率将使用以下命令计算:

power <- mean(output_final$lm.pvalue <= 0.05)

我该怎么做?

您可以尝试-

main_fn <- function() {
test_A <- data.frame(matrix(rnorm(100), nr=10, nc=10))
# independent dataset
test_B <- data.frame(matrix(sample(c(0,1,2), 500, replace = TRUE), nr=50, nc=10))
# Find all combination using dependent and independe datasets's variables
A_B_pair <- subset(expand.grid(c1=names(test_A), c2=names(test_B), 
stringsAsFactors = FALSE))

output <- mapply(function(x, y) test_function(test_A, test_B, x, y), 
A_B_pair$c1, A_B_pair$c2)
output.data <- data.frame(t(output))
output_final <- cbind(A_B_pair, output.data)
}
test_function <- function(test_A, test_B, x,y){
c1 <- test_A[[x]]
c2 <- test_B[[y]]
Data <- data.frame(1, XX=c1, YY=c2)

model_lm <- lm(YY ~ XX, Data)
est_lm <- as.numeric(model_lm$coefficients)[2]
pvalue_lm <- as.numeric(summary(model_lm)$coeffi[,4][2])

return(unlist(data.frame(lm.estimator = est_lm, lm.pvalue =pvalue_lm)))
}
result <- do.call(rbind, replicate(100, main_fn(), simplify = FALSE))
power <- mean(result$lm.pvalue <= 0.05)

最新更新