对多个变量重复执行功能

时间:2019-03-25 10:35:51

标签: r

我需要对43个变量重复一个函数。更具体地说,我正在对数据进行非参数重复测量测试。这将产生两个最终数据集-一个数据集包含每个变量的整体测试统计信息,第二个数据集包含每对时间的特定测试以显示差异在哪里。

我已经创建了用于循环和该函数的变量列表,但是不确定如何使该函数在所有变量上运行。

我将非常感谢您对以下方面的帮助: 1.在每个变量上运行我的函数 2.将每个变量的结果组合成两个数据帧(代码中突出显示的一个用于整体测试,第二个用于特定的时差)。

非常感谢!

这是我的数据:

> dput(junk)
structure(list(Patient = c("TLA001", "TLA001", "TLA001", "TLA001", 
"TLA001", "TLA001", "TLA002", "TLA002", "TLA002", "TLA002", "TLA002", 
"TLA003", "TLA003", "TLA003", "TLA003", "TLA003", "TLA003", "TLA004", 
"TLA004", "TLA004", "TLA004", "TLA004", "TLA004", "TLA005", "TLA005", 
"TLA005", "TLA005", "TLA005", "TLA005", "TLA006", "TLA006", "TLA006", 
"TLA006", "TLA006", "TLA006", "TLA006", "TLA008", "TLA008", "TLA008", 
"TLA008", "TLA008", "TLA008", "TLA009", "TLA009", "TLA009", "TLA009", 
"TLA009", "TLA009", "TLA010", "TLA010", "TLA010", "TLA010", "TLA010", 
"TLA010", "TLA011", "TLA011", "TLA011", "TLA011", "TLA011", "TLA011", 
"TLA012", "TLA012", "TLA012", "TLA012", "TLA012", "TLA012", "TLA012", 
"TLA013", "TLA013", "TLA013", "TLA013", "TLA013", "TLA013", "TLA013", 
"TLA014", "TLA014", "TLA014", "TLA014", "TLA015", "TLA015", "TLA015", 
"TLA015", "TLA015", "TLA015"), Night = c("TLA -3", "TLA -2", 
"TLA -1", "TLA 1", "TLA 2", "TLA 3", "TLA -3", "TLA -2", "TLA -1", 
"TLA 1", "TLA 2", "TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA 2", 
"TLA 3", "TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA 2", "TLA 3", 
"TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA 2", "TLA 3", "TLA -3", 
"TLA -2", "TLA -1", "TLA 1", "TLA 2", "TLA 3", "TLA 4", "TLA -3", 
"TLA -2", "TLA -1", "TLA 1", "TLA 2", "TLA 3", "TLA -3", "TLA -2", 
"TLA -1", "TLA 1", "TLA 2", "TLA 3", "TLA -3", "TLA -2", "TLA -1", 
"TLA 1", "TLA 2", "TLA 3", "TLA -3", "TLA -2", "TLA -1", "TLA 1", 
"TLA 2", "TLA 3", "TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA 2", 
"TLA 3", "TLA 4", "TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA 2", 
"TLA 3", "TLA 4", "TLA -3", "TLA -2", "TLA -1", "TLA 1", "TLA -3", 
"TLA -2", "TLA -1", "TLA 1", "TLA 2", "TLA 3"), var = c(0.1, 
0, 0, 0, 0, 0, 0.5, 0, 0, 2.8, 0.2, 0.3, 0, 0.4, 0.3, 0, 2.2, 
1.1, 0.6, 2.1, 1.4, 1.7, 0.9, 0.3, 0.3, 0, 1, 0, 0.3, 0, 0, 0, 
0, 0, 0, 0, 0.1, 0.2, 0.1, 0.1, 0.4, 0, 0.4, 0.1, 0, 0, 0, 0, 
1.1, 0.8, 0, 0, 0.6, 0, 0, 0, 0, 0, 0, 0, 2, 0.9, 0.2, 0.4, 0.3, 
1, 0.9, 0, 0, 0, 0.3, 0.3, 0, 0, 1.2, 0.9, 0.6, 0.7, 0.7, 0, 
0, 0, 0, 0), SF_Cluster = c("Responder", "Responder", "Responder", 
"Responder", "Responder", "Responder", "Non-responder", "Non-responder", 
"Non-responder", "Non-responder", "Non-responder", "Responder", 
"Responder", "Responder", "Responder", "Responder", "Responder", 
"Responder", "Responder", "Responder", "Responder", "Responder", 
"Responder", "Responder", "Responder", "Responder", "Responder", 
"Responder", "Responder", "Non-responder", "Non-responder", "Non-responder", 
"Non-responder", "Non-responder", "Non-responder", "Non-responder", 
"Responder", "Responder", "Responder", "Responder", "Responder", 
"Responder", "Non-responder", "Non-responder", "Non-responder", 
"Non-responder", "Non-responder", "Non-responder", "Responder", 
"Responder", "Responder", "Responder", "Responder", "Responder", 
"Responder", "Responder", "Responder", "Responder", "Responder", 
"Responder", "Non-responder", "Non-responder", "Non-responder", 
"Non-responder", "Non-responder", "Non-responder", "Non-responder", 
"Responder", "Responder", "Responder", "Responder", "Responder", 
"Responder", "Responder", "Non-responder", "Non-responder", "Non-responder", 
"Non-responder", "Responder", "Responder", "Responder", "Responder", 
"Responder", "Responder")), row.names = c(NA, -84L), class = c("tbl_df", 
"tbl", "data.frame"), spec = structure(list(cols = list(Patient = structure(list(), class = c("collector_character", 
"collector")), Night = structure(list(), class = c("collector_character", 
"collector")), var = structure(list(), class = c("collector_double", 
"collector")), SF_Cluster = structure(list(), class = c("collector_character", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector"))), class = "col_spec"))

这是我的代码:

names(junk)
varlist=names(junk)[5:9]

library("nparLD")

nonparLD=function(var, data=junk) {
  boxplot(var  ~ Night, data = data, lwd = 2, xlab = "time", ylab="N",
        font.lab = 2, cex.lab = 2, main = "Box Plots: var  ")

ex.f1np <- nparLD(var  ~ T, data = data, subject = "id",
                  description = FALSE)
plot(ex.f1np,main = "Relative Effects: var  " )
summary(ex.f1np)

#Data frame 1: Overall test result
r_var=rbind(ex.f1np$ANOVA.test,ex.f1np$Wald.test )
rownames(r_var)=c("varAT","var_WT")


#Data frame 2: which time differences are significant?

m1_2 <- which(((data$T == 1) + (data$T == 2)) == 1)
m1_3 <- which(((data$T == 1) + (data$T == 3)) == 1)
m1_4<- which(((data$T == 1) + (data$T == 4)) == 1)
m1_5<- which(((data$T == 1) + (data$T == 5)) == 1)
m1_6<- which(((data$T == 1) + (data$T == 6)) == 1)


ex.f1np1_2 <- nparLD(var ~ T, data = data[m1_2,], subject = "id",
                     description = FALSE)

ex.f1np1_3 <- nparLD(var ~ T, data = data[m1_3,], subject = "id",
                     description = FALSE)

ex.f1np1_4 <- nparLD(var ~ T, data = data[m1_4,], subject = "id",
                     description = FALSE)

ex.f1np1_5 <- nparLD(var ~ T, data = data[m1_5,], subject = "id",
                     description = FALSE)

ex.f1np1_2 <- nparLD(var ~ T, data = data[m1_2,], subject = "id",
                     description = FALSE)

ex.f1np1_6 <- nparLD(var ~ T, data = data[m1_6,], subject = "id",
                     description = FALSE)


summary(ex.f1np1_2)
summary(ex.f1np1_3)
summary(ex.f1np1_4)
summary(ex.f1np1_5)
summary(ex.f1np1_6)


#Data set for test stats
r_varT=rbind(ex.f1np1_2$ANOVA.test,ex.f1np1_2$Wald.test,
                      ex.f1np1_3$ANOVA.test,ex.f1np1_3$Wald.test,
                      ex.f1np1_4$ANOVA.test,ex.f1np1_4$Wald.test,
                      ex.f1np1_5$ANOVA.test,ex.f1np1_5$Wald.test,
                      ex.f1np1_6$ANOVA.test,ex.f1np1_6$Wald.test)

#Data set for time differences for each var
r_varT=t(r_varT)
colnames(r_varT)=c("AT12","WT12","AT13","WT13","AT14","WT14","AT15","WT15","AT16","WT16" )
rownames(r_varT)=c("varS", "varDF","varP")
}

0 个答案:

没有答案