[R] cv.glm function error message in a loop
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Feb 16 08:21:21 CET 2006
Please study the help page for cv.glm. You have not specified `data'
correctly in the loop case. The help page says
Arguments:
data: A matrix or data frame containing the data. The rows should
be cases and the columns correspond to variables, one of
which is the response.
glmfit: An object of class '"glm"' containing the results of a
generalized linear model fitted to 'data'.
^^^^^^^^^^^^^^^^
and that is not what you specified.
On Thu, 16 Feb 2006, Maria Beger wrote:
> 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/
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list