My data looks something like this:
patient <- c(1,2,3,4,5) outcome1 <- c(rnorm(5)) outcome2 <- c(rnorm(5)) outcome3 <- c(rnorm(5)) outcome4 <- c(rnorm(5)) outcome5 <- c(rnorm(5)) exposure1 <- c(rnorm(5)) exposure2 <- c(rnorm(5)) exposure3 <- c(rnorm(5)) exposure4 <- c(rnorm(5)) exposure5 <- c(rnorm(5)) covariate1 <- c(rnorm(5)) covariate2 <- c(rnorm(5)) data <- data.frame(patient <- patient, outcome1 <- outcome1, outcome2 <- outcome2, outcome3 <- outcome3, outcome4 <- outcome4, outcome5 <- outcome5, exposure1 <- exposure1, exposure2 <- exposure2, exposure3 <- exposure3, exposure4 <- exposure4, exposure5 <- exposure5, covariate1 <- covariate1, covariate2 <- covariate2) I am using the following function to conduct a patrial correlation test and spit out the outcome. This function works great when subsetting a value at a time.
pcor.fit <- function(outcome, exposure, data, cov.columns){ temp <- pcor.test(data[,outcome], data[,exposure], as.matrix(data[,cov.columns])) temp1 <- as.numeric(temp["estimate"]) temp2 <- as.numeric(temp["estimate"]/temp["statistic"]) ## se temp3 <- as.numeric(temp["p.value"]) return(c(outcome = outcome, exposure = exposure, estimate=temp1, se=temp2, p=temp3)) } The only problem is that I want to get partial combinations of all possible combinations of outcome a exposure. In this case it would be 25 (5 exposure and 5 outcomes). therefore I ran a loop to run through the combination of outcome and exposures, where outcome and exposures are lists of the variable names.
for (i in outcome) { for (j in exposure) { print(pcor.fit(outcome = i, exposure = j, data = data, cov.columns = covariates)) } } This works fine in printing the results, but how can I save the results of my function and loop? I assume I need to create an empty matrix first?
pcor.testis no standard function. (2) You are looping overoutcomeandexposurebut there are onlyoutcome1tooutcome5andexposure1toexposure5defined. How are they connected? The same forcovariates. In this form this is no reproducible example.