Hello Ben,

I believe the problem is not strictly update() in the for loop, but
the formula being passed to update().  The value of "i" changes, but
its symbol does not, so the second time through its like you overwrite
the first.  Here is one way around it that works for me:


mdat <- matrix(c(1,2,3, 11,23,13, 12,4,8), nrow = 3, ncol=3, byrow=TRUE)
reg <- lm(mdat[7:9]~1)
for(i in 1:2) {
  x <- paste(". ~ . + mdat[", ((i-1)*3 + 1), ":", (i * 3), "]", sep ="")
  print(x) # just to see it
  reg <- update(reg, x) #update reg twice
}
reg


I am guessing this is a simpler example of what you are really trying
to do, but ff you are not interested in doing anything with reg
between updates, you might consider just update()ing it once with the
full model.

Hope that helps,

Josh

P.S. I really tried to implement a suggestion for a better way to
create formulae by Douglas Bates (see
http://groups.google.com/group/ggplot2/msg/3169aee8708e5e0c? ), but
was spectacularly unsuccessful.

On Sat, Sep 18, 2010 at 9:30 AM, Benjamin Godlove <bgodl...@oberlin.edu> wrote:
> Hi,
>
> First let me say I am a big fan of R and appreciate all your time and
> effort.
>
> The update() function does not seem to work in a for loop.  Consider the
> following:
>
> mdat <- matrix(c(1,2,3, 11,23,13, 12,4,8), nrow = 3, ncol=3, byrow=TRUE)
> reg <- lm(mdat[7:9]~1)
> for(i in 1:2) {
> reg <- update(reg,.~.+mdat[((i-1)*3 + 1):(i*3)]) #update reg twice
> }
> reg # reg should have two independent variables, but it only has one
>
>
>
> The update() function in conjunction with a for loop will only consider the
> last update, and not save all the previous ones.  Is there a way around
> this?
>
> I appreciate your time.
>
> - Ben Godlove
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

______________________________________________
R-help@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.

Reply via email to