Multiple Loop for linear model in R

I am trying to create a loop to do linear model, but need some help to do this. I have two dataframe. The subset of first dataframe looks like (i.e. df1)

enter image description here

And the subset of second dataframe looks like (i.e., df2)-

enter image description here

I am trying to do linear model using "BW" by keeping "Time" as co-variate from df1 with "ZX1" of "df2". Same for all the columns of "df2", like ZX2, ZX3.... and do it for "Length", "Sex" and many more from "df1". In the final data frame, I am interested in extracting the p-value only.

So far, what I have done is-

library(purrr)
(results <- map_dbl(1:ncol(df2),
                    ~{
                      fit <- lm(df2[[.x]] ~ df1$BW * df1$Time)
                      summary(fit)$coefficients[2,4] #To extract the pvalue
                    }) |> set_names(1:ncol(df2)))
FINAL <- as.data.frame(results)

names(FINAL) <- "BW"

Same for other, that is Length

(results <- map_dbl(1:ncol(df2),
                    ~{
                      fit <- lm(df2[[.x]] ~ df1$Length * df1$Time)
                      summary(fit)$coefficients[2,4] #To extract the pvalue
                    }) |> set_names(1:ncol(df2)))
results <- as.data.frame(results)
FINAL <- cbind(FINAL, results)
colnames(FINAL)[2] <- "Length"

Same for others, that is Sex

(results <- map_dbl(1:ncol(df2),
                    ~{
                      fit <- lm(df2[[.x]] ~ df1$Sex * df1$Time)
                      summary(fit)$coefficients[2,4] #To extract the pvalue
                    }) |> set_names(1:ncol(df2)))
results <- as.data.frame(results)
FINAL <- cbind(FINAL, results)
colnames(FINAL)[3] <- "Sex"

This is the final dataframe, that I wanted. Is there a way to do it all together and save the p-values in a final dataframe instead of doing it one by one?

enter image description here

do_thing <- function(var){
(results <- map_dbl(1:ncol(df2),
                    ~{
                      fit <- lm(df2[[.x]] ~ df1[[var]] * df1$Time)
                      summary(fit)$coefficients[2,4] #To extract the pvalue
                    }) |> set_names(1:ncol(df2)))
FINAL <- as.data.frame(results)

names(FINAL) <- var
}

map_dfc(c("BW","Length","Sex"),
    do_thing)

When I ran the command, I got this-

map_dfc(c("BW","Length","Sex"),
         do_thing)

New names:
• `` -> `...1`
• `` -> `...2`
• `` -> `...3`
# A tibble: 1 × 3
  ...1  ...2   ...3 
  <chr> <chr>  <chr>
1 BW    Length Sex  

my mistake, the do_once function should return Final


names(FINAL) <- var
FINAL
}

That worked.
Because I have several variables in df2 (other than BW, Length, Sex), I guess I can do something like-

coldf <- colnames(df1[,2:10])
map_dfc(coldf,
        do_thing)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.