Gam calculations on groups of data and modeling on an xyplot

Hi there,

I am hoping someone will be able to help me with a problem I have been having. I haven’t used R much so I am sorry if it is really obvious.

I have a dataset that contains population values for certain years. In the dataset there are different ID groups with a number of years and values for each, showing changes over time, each ID being a separate group. I would like to perform the following gam function on each set of data by individual ID within the dataset and then incorporate that into plots of the original data for each ID so the original and modeled data can be compared.

The gam formula I have used previously on data for one specific ID is:

model<-gam(y ~ s(x, k=4))
And the plots that have been made already for each ID plotting population value against year is (a plot for each ID):

all.species.plot <-
pdf(file="All Species Plots.pdf") 				
for (i in seq(along=sp.list)){

The graphs come out fine, I just need to add the model data for each ID to its corresponding plot, so far all I have managed to do is plot the same model values to each graph rather than those specific to each ID group. If anyone knows how I can do the calculations for each ID and then incorporate that into the individual plots I would be very grateful. It has been driving me crazy for days!

Thank you!
I am not sure if I understood your problem are you looking for something to replicate your application of gam to data for each of the ID's. If that is the case you may can accomplish that by using lapply/sapply...
Yes, that sounds about right. Something that can help me to carry out the gam application on those sets of data with unique ID numbers within one dataset. So that I am then able to incorporate the individual sets of results into plots of the original data by ID. Would lapply or sapply be able to do this? I read up on them a bit but I haven't used R much before. I will give it a go, thank you!