Home > other >  Dropped values in nested for loop, with multiple replacement length in R
Dropped values in nested for loop, with multiple replacement length in R

Time:01-21

I would like to test multiple model iterations and compare them using a nested for loop.

Reproducible example using the iris dataframe (below).

As the output (ALL_modData), I would like to generate a dataframe with 4 datapoints for each species (12 total). This works for the 2nd and 3rd species (i= 2 and i=3), but it seems to drop iterations for the first species (i=1). Any ideas of why this is happening and how I might fix it would be greatly appreciated!

data("iris")
head(iris)

i=1
j=1
Species <- as.character(unique(iris$Species))
expval <- c(0.25,0.50,0.75,0.80)

for (i in 1:length(Species)) {
  
  for(j in 1:length(expval)) {
    
    
    # Create dataframes
    currdata <- iris[iris$Species == Species[i],] 
    modData <- as.data.frame(Species[i])
    
    # Run model
    mod <- nls(Sepal.Width ~ b1*Petal.Width^expval[j],
               data = currdata,
               start = list(b1 = 0)) 
   
     modsum <- summary(mod)
    
    # Save summary statistics 
    modData$expval <- expval[j]
    modData$est <- modsum[["parameters"]][1] 
    modData$stderr <- modsum[["parameters"]][2] 
    modData$tval <- modsum[["parameters"]][3] 
    modData$pval <- modsum[["parameters"]][4] 
    
    # Bind together in a dataframe 
    if (i==1) {
      ALL_modData = modData
    } else {
      ALL_modData = rbind(ALL_modData,modData)
    }}}

CodePudding user response:

Thanks for providing a reproducible example. I took the liberty and moved things around a bit and removed what I thought was not absolutely needed. At the same time, I believe the readability has been slightly improved if not preserved.

expval <- c(0.25,0.50,0.75,0.80)

result <- list()

counter <- 0
for (species in unique(iris$Species)) {
  for(expval_j in expval) {
    counter <- counter   1  # needed to accumulate data in the list
    
    currdata <- iris[iris$Species == species,] 
    
    mod <- nls(Sepal.Width ~ b1*Petal.Width^expval_j,
               data = currdata,
               start = list(b1 = 0)) 
    
    modsum <- summary(mod)
    
    result[[counter]] <- data.frame(
      species = species,
      expval = expval_j,
      est = modsum[["parameters"]][1],
      stderr = modsum[["parameters"]][2],
      tval = modsum[["parameters"]][3],
      pval = modsum[["parameters"]][4]
    )
  }
}

result <- do.call("rbind", args = result)
result



      species expval      est     stderr     tval         pval
1      setosa   0.25 4.904970 0.09226910 53.15941 5.391081e-45
2      setosa   0.50 6.805375 0.20345691 33.44873 2.102231e-35
3      setosa   0.75 9.153445 0.39931471 22.92289 7.606055e-28
4      setosa   0.80 9.676470 0.45036653 21.48577 1.369816e-26
5  versicolor   0.25 2.590398 0.03371794 76.82553 9.736696e-53
6  versicolor   0.50 2.412266 0.02866094 84.16562 1.150592e-54
7  versicolor   0.75 2.237280 0.02908471 76.92289 9.155387e-53
8  versicolor   0.80 2.202793 0.02964109 74.31552 4.890900e-52
9   virginica   0.25 2.499099 0.03334678 74.94273 3.251383e-52
10  virginica   0.50 2.092763 0.02719387 76.95715 8.959373e-53
11  virginica   0.75 1.746654 0.02502303 69.80184 1.024634e-50
12  virginica   0.80 1.683973 0.02485225 67.75938 4.327017e-50

  • Related