s-news
[Top] [All Lists]

update() function does not work in a for() loop

To: <s-news@lists.biostat.wustl.edu>
Subject: update() function does not work in a for() loop
From: "Russell Ivory" <Russell.Ivory@MerrickBank.com>
Date: Wed, 25 Jan 2006 08:56:10 -0700
Cc: "Scott Wardle" <Scott.Wardle@MerrickBank.com>, "Keith McMillan" <Keith.McMillan@MerrickBank.com>, "Mark Kirkham" <Mark.Kirkham@MerrickBank.com>, "David Wiltsee" <david.wiltsee@MerrickBank.com>
Thread-index: AcYhx9wqocHmq7EKSr6CcRg9Xd1Ajg==
Thread-topic: [S] update() function does not work in a for() loop
I have a function that has worked in the past but for some reason no
longer works.  The function searches for combinations of interactions
that might be useful in a logistic regression model. It now gives me an
error by not recognizing arguments for the update() function when used
in a for() loop.  

The function requires an initial model using Harrell's lrm().  It also
calls the combn() and nCm() functions from the combinations package by
Scott Chasalow.

A small data set, initial model, and a portion of the function are given
below.

fall <- c(1,1,0,1,1,0,0,1,1,1,0,0,1,1,0)
difficulty <- c(3,1,1,2,3,2,1,3,2,2,2,2,3,2,3)
season <- c(1,1,3,3,2,2,2,1,3,1,2,3,2,2,1)
test1 <- c(6,4,9,2,5,7,4,2,5,2,4,2,5,6,8)
test2 <- c(8,9,4,5,3,1,5,2,4,5,1,6,8,5,8) 
test3 <- rnorm(15,6,1.2)
testdata <- data.frame(cbind(fall,difficulty,season,test1,test2,test3))
test4 <- testdata[,c(4:6)]
testmodel <- lrm(fall~difficulty+season,data = testdata) 


inttest <- function(model, varlist)
{
        p <- c(1:dim(varlist)[2])
        combosp <- t(combn(p, 2))
        combos <- t(combn(c(names(varlist)), 2))
        modeltest <- list()
        vara <- list()
        varb <- list()
                for(a in 1:dim(combos)[1])
                {
                vara[[a]] <- varlist[, combos[a, 1]]
                varb[[a]] <- varlist[, combos[a, 2]]
                modeltest[[a]] <- update(model,  ~ . + vara[[a]] *
varb[[a]])
                }
        return(modeltest)
}

This runs with the following error:
> inttest(testmodel, test4)
Problem in inttest(testmodel, test4): Object "vara" not found 
Use traceback() to see the call stack


Thus, it does not recognize "vara" in the for() loop.  However, "vara"
does exist because I can ask for something simple and it works just
fine.

inttest <- function(model, varlist)
{
        p <- c(1:dim(varlist)[2])
        combosp <- t(combn(p, 2))
        combos <- t(combn(c(names(varlist)), 2))
        addtest <- list() # Added in place of modeltest <- list()
        vara <- list()
        varb <- list()
                for(a in 1:dim(combos)[1])
                {
                vara[[a]] <- varlist[, combos[a, 1]]
                varb[[a]] <- varlist[, combos[a, 2]]
                addtest[[a]] <- vara[[a]] + varb[[a]] # In place of
modeltest
                #modeltest[[a]] <- update(model,  ~ . + vara[[a]] *
varb[[a]])
                }
        return(addtest) # Added in place of modeltest
}

> inttest(testmodel, test4)
[[1]]:
 [1] 14 13 13  7  8  8  9  4  9  7  5  8 13 11 16

[[2]]:
 [1] 12.770567 11.475442 15.759322  5.574777 13.339220 10.272851
9.841596  8.048929
 [9] 10.153110  7.608980 10.459834  8.815925 10.484757 12.247411
13.063484

[[3]]:
 [1] 14.770567 16.475442 10.759322  8.574777 11.339220  4.272851
10.841596  8.048929
 [9]  9.153110 10.608980  7.459834 12.815925 13.484757 11.247411
13.063484


Most puzzling about this problem is that it worked in the past.  I've
also tried making "vara" a matrix and selecting columns for each loop
but I get the same result in not recognizing "vara". Any thoughts would
be appreciated.

Russell Ivory | AVP Modeling Manager | Merrick Bank | (801) 545-6640 |
russell.ivory@merrickbank.com

10705 South Jordan Gateway
Suite 200
South Jordan, UT 84095
****************************************************************************This
 e-mail and any files transmitted with it are confidential and are intended 
solely for the use of the individual or entity to whom it is addressed. If you 
are not the intended recipient or the person responsible for delivering the 
e-mail to the intended recipient, be advised that you have received this e-mail 
in error, and that any use, dissemination, forwarding, printing, or copying of 
this e-mail is strictly prohibited. If you received this e-mail in error, 
please return the e-mail to the sender at Merrick Bank and delete it from your 
computer. Although Merrick Bank attempts to sweep e-mail and attachments for 
viruses, it does not guarantee that either are virus-free and accepts no 
liability for any damage sustained as a result of viruses.


<Prev in Thread] Current Thread [Next in Thread>
  • update() function does not work in a for() loop, Russell Ivory <=