Skip to content

loop function

2 messages · fatimah soleimany, Adams, Jean

#
Hi Dear users,
for an interactive use, i am trying to write a loop that looks for all
variables in the conditions which i introduced, this is what I'm trying:

library(MASS)> i=1> for (i in 58:1){+ for(j in 58:i){+ + str.temp <-
paste("y1 ~ x", i, "* x", j, sep = "")+
univar<-glm.nb(as.formula(str.temp), data=df)+
b=summary(univar)$coeffients[4,4]+ b<-c(i,j)+ if(b < 0.6){ +
print(b)+    }+   }+   }

i have no error but i didn't get true response too, my result is this:


b[1] 1 1

 but i want all the variables that their p-value are less than 0.6, i
think i couldn't write a true loop,can you help me?

thank you in advance for any help
#
You seemed to have re-written over the "b" object in your code.
This might work for you.

library(MASS)
for (i in 58:1){
  for(j in 58:i){
    str.temp <- paste("y1 ~ x", i, "* x", j, sep = "")
    univar <- glm.nb(as.formula(str.temp), data=df)
    b <- summary(univar)$coeffients[4, 4]
    if(b < 0.6){
      cat(i, j, b, "\n")
    }
  }
}

By the way, you should post using plain text (not html).

Jean


On Thu, Aug 18, 2016 at 6:48 AM, fatimah soleimany <
fatimah.soleimany at gmail.com> wrote: