s-news
[Top] [All Lists]

Re: [S] gam(lo()) and predict.gam()

To: s-news@wubios.wustl.edu, takezawa@affrc.go.jp
Subject: Re: [S] gam(lo()) and predict.gam()
From: Prof Brian Ripley <ripley@stats.ox.ac.uk>
Date: Mon, 29 May 2000 12:29:04 +0100 (BST)
Reply-to: Prof Brian Ripley <ripley@stats.ox.ac.uk>
Sender: owner-s-news@wubios.wustl.edu

>From: Takezawa Kunio <takezawa@affrc.go.jp>
>Date: Mon, 29 May 2000 20:13:43 +0900
>To: s-news@wubios.wustl.edu
>Subject: Re: [splus-users,14046]  [S] gam(lo()) and predict.gam()
>X-loop: s-news
>
>
>Hi! S-plus users.
>E-mail: s-news@wubios.wustl.edu
>
>   Let me modify the program I wrote in my previous
>article. The two programs below work well.

[The second function is incorrect and the first is dangerous.]

>I have heard that "<<-" and "rm())" are preferable
>to "attach()" and "detach()". But I do not know
>why.

Definitely not!  You should be assigning in frame 1, not in the
permanent database (and thereby clobbering any file data1 that
existed there).  It is not good practice to write in the permanent
database. On the other hand, your second solution did not work for me
as it found the wrong x.  Unless you specify a data frame for the
variables you run the danger of getting any old x that is in view.
So if you have x in frame 1, frame 0 or the working directory,
you will get the wrong answer.

Here is a better style:

testit <- function()
{
    xx <- seq(from = 1, by = 1, to = 10)
    ex <- seq(from = 3, by = 0.3, length = 12)
    yy <- sin(xx) + 2
    assign("data1", data.frame(x = xx, y = yy), frame=1)
    formula.name <- substitute(y ~ lo(x, span=span1), list(span1=0.4))
    fit.gam <- gam(formula.name, data = data1, family = "poisson",
                   x = T, model = T)
    print(fit.gam$call)
    data2 <- data.frame(x = ex)
    ey <- predict.gam(fit.gam, data2)
    ey
}

using substitute as it is intended for this.


>-------------------- Program (C) --------------------
>function()
>{
>       xx <- seq(from = 1, by = 1, to = 10)
>       ex <- seq(from = 3, by = 0.3, length = 12)
>       yy <- sin(xx) + 2
>       data1 <<- data.frame(x = xx, y = yy)
>       span1 <- 0.4
>       formula.name <- paste("y ~ lo(x, span =", span1, ")")
>       fit.gam <- gam(formula.name, data = data1, family = "poisson", x = T, 
model = T)
>       data2 <- data.frame(x = ex)
>       ey <- predict.gam(fit.gam, data2)
>       rm(data1)
>       ey
>}
>-------------------- End of Program (C) --------------------
>
>
>
>-------------------- Program (D) --------------------
>function()
>{
>       xx <- seq(from = 1, by = 1, to = 10)
>       ex <- seq(from = 3, by = 0.3, length = 12)
>       yy <- sin(xx) + 2
>       data1 <- data.frame(x = xx, y = yy)
>       attach(data1)
>       span1 <- 0.4
>       formula.name <- paste("y ~ lo(x, span =", span1, ")")
>       fit.gam <- gam(formula.name, family = "poisson", x = T, model = T)
>       data2 <- data.frame(x = ex)
>       ey <- predict.gam(fit.gam, data2)
>       detach("data1")
>       ey
>}
>-------------------- End of Program (D) --------------------
>
>   *****    Kunio Takezawa, Ph.D. (takezawa@affrc.go.jp)    *****
>  *****            Research Information Section              *****
> **** Hokuriku National Agricultural Experiment Station, JAPAN ****
>*****     <http://www.inada.affrc.go.jp/~takezawa/patent-e.html>    *****
>-----------------------------------------------------------------------
>This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
>send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
>message:  unsubscribe s-news

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>