Re: [R] new to loops

From: sunny <sunayan_at_gmail.com>
Date: Sun, 08 May 2011 22:50:19 -0700 (PDT)

Maybe this is what you're looking for:

# x is your set of explanatory variables (10 of them): x <- array(rnorm(10000), dim=c(1000,10))

# y is your dependent variable:
y <- rbinom(1000, 1, 0.3)

# run the regression on each column of x: reg <- apply(x, 2, function(z) glm(y ~ z, family=binomial(link='logit')))

# the previous output is a list. Now collect whatever you need from each list element:
bond <- lapply(reg, function(z) c(z$coeff[2], exp(z$coeff[2]), confint(z)[2,], exp(confint(z)[2,])))

# collect everything together as a matrix: bond <- do.call(rbind, bond)
bond

-S.

SevannaD wrote:

> 
> So in my first try before I got your message, this is what I did:
> 
> orconf<-list()
>  ccoef<-list()
>  or<-list()
>  coef<-list()
>  out<-list()
>  for (i in 1:49){
> out[[i]]<-glm(y~var[[i]],family=binomial(link="logit"))
> coef[[i]]<-out[[i]]$coef[2]
>  or[[i]]<-exp(out[[i]]$coef[2])
>  bond<-matrix(out[[i]]$coef[2],
> exp(out[[i]]$coef[2]),confint(out[[i]]$coef[2]),exp(confint(out[[i]]$coef[2])))
> }
> But it did not work due to confint(out[[i]]$coef[2] and the exp one. Said
> Error in object$coefficients : $ operator is invalid for atomic vectors.
> 
> would I would to identify conf_low and conf_high as two separate things?
> 
> Thanks
> 
> On Sun, May 8, 2011 at 4:31 PM, Scott Chamberlain-3 [via R] <
> ml-node+3508106-1763482049-235888_at_n4.nabble.com> wrote:
> 

>> Not knowing what format your data is in or what model you are using...
>>
>> df # is your data frame with columns the variables you are running
>> regressions for
>> datout <- data.frame(coeff = NA, conf_low = NA, conf_high = NA, odd = NA)
>> #
>> a table to put your results in
>> for(i in 1:length(names(df)[2:10])) {
>> fit <- glm(data[,1] ~ data[,i], data = df, etc...)
>> datout[i,] <- fit[e.g, 1:4] # determine what values in your model output
>> are what you need
>> }
>> datout # a table with all your output for each variable
>>
>> On Sunday, May 8, 2011 at 11:58 AM, SevannaD wrote:
>>
>> I have never made a loop on my own to do anything in R. But I am hoping
>>
>> > someone can help me build one for the following issue:
>> >
>> > I need to make a univariate logistic regression for each of my
>> variables
>> > (about 62 of them), then I need to gather up each of their coefficients
>> (not
>> > the intercepts), each of their 95% confidence intervals, and each of
>> thier
>> > odds ratios and place them in a matrix to showcase them for my thesis.
>> >
>> > currently, I am writing them all out one by one with the cbond method,
>> which
>> > has taken me a better part of a day so far and I know there has to be
>> able
>> > to be a way to make a loop that can do this whole process, I just
>> havent
>> > been able to figure it out yet.
>> >
>> > Thanks in advance.
>> >
>> > --
>> > View this message in context:
>> http://r.789695.n4.nabble.com/new-to-loops-tp3507366p3507366.html&lt;http://r.789695.n4.nabble.com/new-to-loops-tp3507366p3507366.html?by-user=t&gt;
>> > Sent from the R help mailing list archive at Nabble.com.
>> >
>> > ______________________________________________
>> > [hidden
>> email]&lt;http://user/SendEmail.jtp?type=node&amp;node=3508106&amp;i=0&amp;by-user=t&gt;mailing
>> list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt;
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden
>> email]&lt;http://user/SendEmail.jtp?type=node&amp;node=3508106&amp;i=1&amp;by-user=t&gt;mailing
>> list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html&lt;http://www.r-project.org/posting-guide.html&gt;
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>> ------------------------------
>> If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://r.789695.n4.nabble.com/help-with-a-vector-loop-problem-tp3507366p3508106.html
>> To unsubscribe from help with a vector loop problem, click
>> here&lt;http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&amp;node=3507366&amp;code=c2V2YW5uYWRhcmtzdGFyQGdtYWlsLmNvbXwzNTA3MzY2fC0xMzE0Mjc1OTM3&gt;.
>>
>>
>
--
View this message in context: http://r.789695.n4.nabble.com/help-with-a-vector-loop-problem-tp3507366p3508483.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Mon 09 May 2011 - 06:05:46 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 09 May 2011 - 06:40:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive