[R] cv.glm function error message in a loop

Maria Beger mbeger at zen.uq.edu.au
Thu Feb 16 06:20:14 CET 2006


Dear list,
I am modelling fish distributions using the glm-function followed by the
step-function, and then want to cross-validate the model via the
cv.glm-function from the {boot} package. I am working on fish
distributions on coral reefs.  The code I have works for one fish
species. Since I have 227 fishes, I wrote a loop.  Now the
cv.glm-function comes up with an error message: "Error in
model.frame(formula, rownames, variables, varnames, extras, extranames,
: variable lengths differ" and I cannot work out what the problem is.
All the data are the same length and I double-checked all.  I think its
to do with how cv.glm addresses the fish data columns for the
crossvalidation within "data".  

Any help would be appreciated, the code is below.  I can send more
detail if required. 

Thank you very much for your help!!!  Maria



"data" is a dataframe with the first 227 columns with fish
presence/absence data (=response), then 10 columns of predictor
variables.

Code that doesn't work:

for (i in 1:length(datafish))  #this is 227 columns within "data"
{
#run glm & step of fish.[i] with all parameters no interactions
modelfish<-glm(data[,i]~ECO.NAME+ISLAND+DISTANCE.EST+DEPTH500M,
binomial)
stepmodfi<-step(modelfish,trace=1,scope=list(lower= ~1,
upper=~ECO.NAME+ISLAND+DISTANCE.EST+DEPTH500M) ,direction="both")
#run cross-validation
fish.vali<-cv.glm(data,stepmodfi)
}

Code for one fish that works:
modelfish.355<-glm(fi355~ECO.NAME+ISLAND+DISTANCE.EST+DEPTH500M,
binomial, data=data)
stepmodfi.355<-step(modelfish.355,trace=1,scope=list(lower= ~1,
upper=~ECO.NAME*ISLAND*DISTANCE.EST*DEPTH500M) ,direction="both")
fish.vali<-cv.glm(data,stepmodfi.355)


Maria Beger
PhD Candidate
The Ecology Centre
School of Integrative Biology
University of Queensland
St Lucia, QLD 4072, Australia
Tel: +61 7 3365 1671
mbeger at zen.uq.edu.au
http://www.uq.edu.au/spatialecology/  

NRAS - Marshall Islands
Natural Resource Assessment Surveys Project 
www.nras-conservation.org

Chuuk Lagoon Project
http://www.earthwatch.org/




More information about the R-help mailing list