Hi R community! I am analyzing the data set "motorins" in the package "faraway" by using the generalized additive model. it shows the following error. Can some one suggest me the right way? library(faraway) data(motorins) motori <- motorins[motorins$Zone==1,] library(mgcv)>amgam <- gam(log(Payment) ~ offset(log(Insured))+s(as.numeric(Kilometres)) + s(Bonus) + Make + s(Claims),family = gaussian, data = motori) Error in smooth.construct.tp.smooth. spec(object, dk$data, dk$knots) : A term has fewer unique covariate combinations than specified maximum degrees of freedom> summary(amgam)Error in summary(amgam) : object 'amgam' not found Gyan [[alternative HTML version deleted]]
Gyanendra Pokharel wrote on 11/08/2011 10:57:38 AM:> > Hi R community! > I am analyzing the data set "motorins" in the package "faraway" by using > the generalized additive model. it shows the following error. Can someone> suggest me the right way? > > library(faraway) > data(motorins) > motori <- motorins[motorins$Zone==1,] > library(mgcv) > >amgam <- gam(log(Payment) ~ offset(log(Insured))+ > s(as.numeric(Kilometres)) + s(Bonus) + Make + s(Claims),family =gaussian,> data = motori) > Error in smooth.construct.tp.smooth. > spec(object, dk$data, dk$knots) : > A term has fewer unique covariate combinations than specified maximum > degrees of freedom > > summary(amgam) > Error in summary(amgam) : object 'amgam' not found > GyanYou could try reducing the number of degrees of freedom in the smooth terms with the k argument of the s() function: amgam <- gam(log(Payment) ~ offset(log(Insured))+ s(as.numeric(Kilometres), k=3) + s(Bonus, k=3) + Make + s(Claims, k=3), data=motori) Jean [[alternative HTML version deleted]]
Kilometres has only 5 unique values, while Bonus has only 7, but the default smoothing basis dimension for the s terms is 10, so there is a problem. Solution is to reduce the basis dimension. e.g. amgam <- gam(log(Payment) ~ offset(log(Insured))+ + s(as.numeric(Kilometres),k=5) + s(Bonus,k=7) + Make + s(Claims),family = gaussian, + data = motori) On 08/11/11 16:57, Gyanendra Pokharel wrote:> Hi R community! > I am analyzing the data set "motorins" in the package "faraway" by using > the generalized additive model. it shows the following error. Can some one > suggest me the right way? > > library(faraway) > data(motorins) > motori<- motorins[motorins$Zone==1,] > library(mgcv) >> amgam<- gam(log(Payment) ~ offset(log(Insured))+ > s(as.numeric(Kilometres)) + s(Bonus) + Make + s(Claims),family = gaussian, > data = motori) > Error in smooth.construct.tp.smooth. > spec(object, dk$data, dk$knots) : > A term has fewer unique covariate combinations than specified maximum > degrees of freedom >> summary(amgam) > Error in summary(amgam) : object 'amgam' not found > Gyan > > [[alternative HTML version deleted]] > > ______________________________________________ > 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
On 11/08/2011 11:57 AM, Gyanendra Pokharel wrote:> Hi R community! > I am analyzing the data set "motorins" in the package "faraway" by using > the generalized additive model. it shows the following error. Can some one > suggest me the right way? > > library(faraway) > data(motorins) > motori<- motorins[motorins$Zone==1,] > library(mgcv) > amgam<- gam(log(Payment) ~ offset(log(Insured))+ > s(as.numeric(Kilometres)) + s(Bonus) + Make + s(Claims),family = gaussian, > data = motori) > Error in smooth.construct.tp.smooth. > spec(object, dk$data, dk$knots) : > A term has fewer unique covariate combinations than specified maximum > degrees of freedomJust to provide further information, > table(motori$Kilometres) 1 2 3 4 5 61 63 60 57 54 Kilometres only has 5 unique values; this complicates the issue of estimating a curve, as you only have 5 points to work with. Likewise, Bonus only has 7 unique values. Claims does not pose any problem, as it has 98 unique values. As Jean suggested, you can get around this issue with: amgam<- gam(log(Payment) ~ offset(log(Insured)) + s(as.numeric(Kilometres),k=5) + s(Bonus,k=7) + Make + s(Claims),family = gaussian, data = motori) -- Patrick Breheny Assistant Professor Department of Biostatistics Department of Statistics University of Kentucky