[R] step.gam with a list of data frames
Kim Vanselow
Vanselow at gmx.de
Tue May 3 18:10:52 CEST 2011
Dear R-helpers,
I used the step.gam function (package gam, Trevor Hastie) on a data frame without problems. Then I created a list of several bootstrap samples from this data frame. Now I want to use the step.gam function on this list using a for-loop. The code is working well until the step.gam function starts. Step.gam is starting but then suddenly breaks (see error below). However, if I replace the for-loop (see below) the whole thing is working well.
for-loop with which the code does not work:
for(i in 1:4){
fit_list[[i]] <- gam(dom_teresken ~ 1, family = poisson, data = Pamir_res[[i]])
}
Replace this with:
fit_list[[1]] <- gam(dom_teresken ~ 1, family = poisson, data = Pamir_res[[1]])
fit_list[[2]] <- gam(dom_teresken ~ 1, family = poisson, data = Pamir_res[[2]])
fit_list[[3]] <- gam(dom_teresken ~ 1, family = poisson, data = Pamir_res[[3]])
fit_list[[4]] <- gam(dom_teresken ~ 1, family = poisson, data = Pamir_res[[4]])
Now everything works well. But I have to use the loop as I want to use much more than only 4 bootstrap samples.
This is the error message:
Start: dom_teresken ~ 1; AIC= 516.6801
Trial: dom_teresken ~ ALTITUDE + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1; AIC= 512.2938
Trial: dom_teresken ~ 1 + SLOPE + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1; AIC= 495.0212
Trial: dom_teresken ~ 1 + 1 + SOUTH_EXPOSEDNESS + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1; AIC= 486.8359
Trial: dom_teresken ~ 1 + 1 + 1 + WEST_EXPOSEDNESS + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1; AIC= 529.8005
Trial: dom_teresken ~ 1 + 1 + 1 + 1 + DISTANCE_TO_ISOBATH + 1 + 1 + 1 + 1 + 1 + 1 + 1Error in Pamir_res[[i]] : subscript out of bounds
This is my full code:
# take bootstrap samples
require(MIfuns) # resample functions
Pamir_res <- resample(Pamir, names= 1:4, replace=TRUE) # take n bootstrap
# samples from data = Pamir
# calculate gam for each bootstrap sample
fit_list <- vector(4, mode = "list") # create empty list
for(i in 1:4){
fit_list[[i]] <- gam(dom_teresken ~ 1, family = poisson, data = Pamir_res[[i]])
}
gam.scope = list(
~ 1 + ALTITUDE + s(ALTITUDE, 2),
~ 1 + SLOPE + s(SLOPE, 2),
~ 1 + SOUTH_EXPOSEDNESS + s(SOUTH_EXPOSEDNESS, 2),
~ 1 + WEST_EXPOSEDNESS + s(WEST_EXPOSEDNESS, 2),
~ 1 + DISTANCE_TO_ISOBATH + s(DISTANCE_TO_ISOBATH, 2),
~ 1 + UTM_NORTHING + s(UTM_NORTHING, 2),
~ 1 + UTM_EASTING + s(UTM_EASTING, 2),
~ 1 + DISTANCE_TO_SETTLEMENT, s(DISTANCE_TO_SETTLEMENT, 2),
~ 1 + NDVI_Rededge_R_Mean + s(NDVI_Rededge_R_Mean, 2),
~ 1 + NDVI_IR_Rededge_t_Mean + s(NDVI_IR_Rededge_t_Mean, 2),
~ 1 + Bd_5_Mean + s(Bd_5_Mean, 2),
~ 1 + Bd_1t_Mean + s(Bd_1t_Mean, 2))
fit_boot <- vector(4, mode = "list") # create empty list
for(i in 1:4){
fit_boot[[i]] <- step.gam(fit_list[[i]], scope = gam.scope, direction = "both", trace = TRUE)
}
I really tried to find suggestions on the internet and in nabble. Unfortunately I could not solve the problem. Please help me!
Thank you very much,
Kim
--
More information about the R-help
mailing list