please help for mgcv package
On Tue, Jun 21, 2011 at 9:20 AM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
Hi, On Tue, Jun 21, 2011 at 11:27 AM, pigpigmeow <glorykwok at hotmail.com> wrote:
i read a book from WOOD, there's an example which is talking about the pollutant.
Read the text carefully!!!
library(gamair) library(mgcv)
data(chicago) # otherwise this isn't reproducible
y<-gam(death~s(time,bs="cr",k=200)+s(pm10median,bs="cr")+s(so2median,bs="cr")+s(o3median,bs="cr")+s(tmpd,bs="cr"),data=chicago,family=Possion)
# this line won't work y<-gam(death~s(time,bs="cr",k=200)+s(pm10median,bs="cr")+s(so2median,bs="cr")+s(o3median,bs="cr")+s(tmpd,bs="cr"),data=chicago,family=poisson())
lag.sum<-function(a,10,11)
but this line is not what is in the book...I know it is not the easiest to see, but those are ls not 1s.
{n<-length(a)
b<-rep(0,n-11)
for(i in 0:(11-10))
b<-b+a[(i+1):(n-11+i)]
At best this risks being confusing, at worst it could lead to errors.
If you use a for loop, it is good practice not to break the line (what
the good Dr. Wood does), or if you prefer to break the line, wrap it
in curly braces {}. See ?for for the documentation.
b}
Here is a copy (following my stylistic preferences, but should be
identical in function):
lag.sum <- function(a, l0, l1) {
n <- length(a)
b <- rep(0, n - l1)
for (i in 0:(l1 - l0)) {
b <- b + a[(i + 1):(n - l1 + i)]
}
b
}
Regarding what rep() does, see the documentation, ?rep also, play
with simple examples to see its behavior:
rep(1, 10)
rep(1:3, 5)
If you are unfamiliar with R, you might have an easier go of it if you
read some of the myriad of online documentation/tutorials (or a book)
designed to introduce you to R. GAMs is not really written to
introduce R, so it will be unnecessarily difficult to learn R from it.
Here is one of the official manuals:
http://cran.r-project.org/doc/manuals/R-intro.html
Patrick Burns also has a number of tutorials for different levels of useRs:
http://www.burns-stat.com/pages/tutorials.html
HTH,
Josh
Why would you pass an unnamed constant to a function, and then try to
change them later? When you use lag.sum(), you try to set those
arguments to other values.
lag.sum<-function(a1, a2, a3)
{
n<-length(a1)
b<-rep(0, n-a3)
for(i in 0:(a2-a3))
b<- b + a1[(i+1):(n-a3+i)]
b
}
death<-chicago$death[4:5114] time<-chicago$time[4:5114] o3<-lag.sum(chicago$o3median,0,3) tmp<-lag.sum(chicago$tmpd,0,3)
Like here. Your function hard-coded the values in, or tried to, and yet you want 0 and 3 now instead of 10 and 11.
pm10<-lag.sum(log(chicago$pm10median+40),0,3) so2<-lag.sum(log(chicago$so2median+10),0,) I don't know what is the script (Bold font ) used for......
This is a plain-text list. Bold font doesn't show up.
and it shows "Error: unexpected numeric constant in "lag.sum<-function(a,10" ", why? anyone can answer me?
I don't have the book you reference, but I wonder if that's really what it says. Sarah -- Sarah Goslee http://www.functionaldiversity.org
______________________________________________ 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.
Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/