Home > Back-end >  How can I extract, label and data.frame values from Console in a loop?
How can I extract, label and data.frame values from Console in a loop?

Time:11-04

I made a nls loop and get values calculated in console. Now I want to extract those values, specify which values are from which group and put everything in a dataframe to continue working.

my loop so far:

for (i in seq_along(trtlist2)) { loopmm.nls <- 
  nls(rate ~ (Vmax * conc /(Km   conc)), 
      data=subset(M3, M3$trtlist==trtlist2[i]),
      start=list(Km=200, Vmax=2),  trace=TRUE )

summary(loopmm.nls)
print(summary(loopmm.nls))
}

the output in console: (this is what I want to extract and put in a dataframe, I have this same "parameters" thing like 20 times)

Parameters:
     Estimate Std. Error t value Pr(>|t|)    
Km   23.29820    9.72304   2.396   0.0228 *  
Vmax  0.10785    0.01165   9.258 1.95e-10 ***
---

different ways of extracting data from the console that work but not in the loop (so far!)

#####extract data in diff ways from nls##### 

## extract coefficients as matrix  

Kinall <- summary(mm.nls)$parameters 

  
## extract coefficients save as dataframe 

Kin <- as.data.frame(Kinall) 

colnames(Kin)  <- c("values", "SE", "T", "P") 

  
###create Km Vmax df 

Kms <- Kin[1, ] 

Vmaxs <- Kin[2, ] 
 

#####extract coefficients each manually 

Km <- unname(coef(summary(mm.nls))["Km", "Estimate"]) 

Vmax <- unname(coef(summary(mm.nls))["Vmax", "Estimate"]) 
 

KmSE <- unname(coef(summary(mm.nls))["Km", "Std. Error"]) 

VmaxSE <- unname(coef(summary(mm.nls))["Vmax", "Std. Error"]) 

  
KmP <- unname(coef(summary(mm.nls))["Km", "Pr(>|t|)"]) 

VmaxP <- unname(coef(summary(mm.nls))["Vmax", "Pr(>|t|)"]) 

  

KmT <- unname(coef(summary(mm.nls))["Km", "t value"]) 

VmaxT <- unname(coef(summary(mm.nls))["Vmax", "t value"]) 

one thing that works if you extract data through append, but somehow that only works for "estimates" not the rest

Kms <- append(Kms, unname(coef(loopmm.nls)["Km"] ))
Vmaxs <- append(Vmaxs, unname(coef(loopmm.nls)["Vmax"] )) 
}
Kindf <- data.frame(trt = trtlist2, Vmax = Vmaxs, Km = Kms)

CodePudding user response:

I would just keep everything in the dataframe for ease. You can nest by the group and then run the regression then pull the coefficients out. Just make sure you have tidyverse and broom installed on your computer.

library(tidyverse)


#example
mtcars |>
  nest(data = -cyl) |>
  mutate(model = map(data, ~nls(mpg~hp^b, 
                                data = .x, 
                                start = list(b = 1))),
         clean_mod = map(model, broom::tidy))  |>
  unnest(clean_mod) |>
  select(-c(data, model))
#> # A tibble: 3 x 6
#>     cyl term  estimate std.error statistic  p.value
#>   <dbl> <chr>    <dbl>     <dbl>     <dbl>    <dbl>
#> 1     6 b        0.618    0.0115      53.6 2.83e- 9
#> 2     4 b        0.731    0.0217      33.7 1.27e-11
#> 3     8 b        0.504    0.0119      42.5 2.46e-15


#what I expect will work for your data
All_M3_models <- M3 |>
  nest(data = -trtlist) |>
  mutate(model = map(data, ~nls(rate ~ (Vmax * conc /(Km   conc)), 
                                data=.x,
                                start=list(Km=200, Vmax=2))),
         clean_mod = map(model, broom::tidy))|>
  unnest(clean_mod) |>
  select(-c(data, model))
  • Related