i read a book from WOOD, there's an example which is talking about the pollutant. library(gamair) library(mgcv) 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) lag.sum<-function(a,10,11) {n<-length(a) b<-rep(0,n-11) for(i in 0:(11-10)) b<-b+a[(i+1):(n-11+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) 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...... and it shows "Error: unexpected numeric constant in "lag.sum<-function(a,10" ", why? anyone can answer me? -- View this message in context: http://r.789695.n4.nabble.com/please-help-for-mgcv-package-tp3614485p3614485.html Sent from the R help mailing list archive at Nabble.com.
it is from Generalized Additive Models an introduction with R by Simon N.Wood . Page 251 -- View this message in context: http://r.789695.n4.nabble.com/please-help-for-mgcv-package-tp3614485p3614535.html Sent from the R help mailing list archive at Nabble.com.
On Jun 21, 2011, at 11:27 AM, pigpigmeow wrote:> i read a book from WOOD, there's an example which is talking about the > pollutant. > > library(gamair) > library(mgcv) > 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) > > lag.sum<-function(a,10,11)Consider: > tf <- function(a, 10){return(10)} Error: unexpected numeric constant in "tf <- function(a, 10" You are attempting to create a function with an unnamed argument. -- David.> {n<-length(a) > b<-rep(0,n-11) > for(i in 0:(11-10)) > b<-b+a[(i+1):(n-11+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) > 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...... > > and it shows "Error: unexpected numeric constant in "lag.sum<- > function(a,10" > ", why? > > anyone can answer me? > > -- > View this message in context: http://r.789695.n4.nabble.com/please-help-for-mgcv-package-tp3614485p3614485.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.David Winsemius, MD West Hartford, CT
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. > > 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) > {n<-length(a) > b<-rep(0,n-11) > for(i in 0:(11-10)) > b<-b+a[(i+1):(n-11+i)] > > b}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
b<-rep(0,n-11) for(i in 0:(11-10)) b<-b+a[(i+1):(n-11+i)] i don't know what does "rep" mean and for-loop! -- View this message in context: http://r.789695.n4.nabble.com/please-help-for-mgcv-package-tp3614485p3615861.html Sent from the R help mailing list archive at Nabble.com.
In the 'inst/scripts' directory of package 'gamair' there are files containing the R code for each chapter of the book (questionable layout uncorrected, but typo free, at least!). Simon On 21/06/11 16:27, pigpigmeow wrote:> i read a book from WOOD, there's an example which is talking about the > pollutant. > > library(gamair) > library(mgcv) > 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) > > lag.sum<-function(a,10,11) > {n<-length(a) > b<-rep(0,n-11) > for(i in 0:(11-10)) > b<-b+a[(i+1):(n-11+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) > 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...... > > and it shows "Error: unexpected numeric constant in "lag.sum<-function(a,10" > ", why? > > anyone can answer me? > > -- > View this message in context: http://r.789695.n4.nabble.com/please-help-for-mgcv-package-tp3614485p3614485.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. >-- Simon Wood, Mathematical Science, University of Bath BA2 7AY UK +44 (0)1225 386603 http://people.bath.ac.uk/sw283