Re: [R] batch job GLM calculations

From: Ken Beath <>
Date: Thu 11 Jan 2007 - 09:06:23 GMT

as.formula(modelstructure[i]) in the glm function

>>> "Indermaur Lukas" <> 01/11/07 7:34 PM >>> Hello

I want to batch job the calculation of many GLM-models, extract some values and store them in a file. Almost everything in the script below works (read file, extract values and write them to file) except I fail in indexing the GLM with the modelstructure it should run. Running GLM's conventionally is no problem.  

Conventionally a GLM is calculated as:

glm(ZlogHRS ~ ZRi+ZE+ZPROX_MN+ZED+ZAlwd+ZT2+ZW+ZN+Sex+y, family = gaussian,

(just a note: dependent variable is ZlogHRS, while the others are indepent variables)  

Desired way: sequentially run GLM

I want R to take the model structure to take from a vector called "modelstructure" and paste it into the GLM like:

glm(modelstructure[i], family = gaussian,

It would considerably ease my workload if there is a solution to the indexing problem within a GLM. I appreciate any hint.

Best regards



my R skills are rather poor  

----------------------------------------START R-CODE-----------------------------------

# Read file

t.url <- "C://HR_calculations/2005_2006/HR_SIZE/Kandidatenmodelle_Berechnung/inputfiles/"

t.tuti <- read.table(paste(t.url, "All_animals.txt", sep=""),header=T)  

collect.results <- function(x) {

#resets vectors which will be filled

i <- 0

AICA <- NA;  

#put models names hierarchically in vector

modelnames <- c("1=global", "2=biotic1", "3=biotic2", "4=abiotic") #keep track of changes in model names and number

for (i in 1:length(modelnames))

#model structure of the four models given for all models to run


modelstructure <- c(

"ZlogHRS ~ ZRi+ZE+ZPROX_MN+ZED+ZAlwd+ZT2+ZW+ZN+Sex+y",


"ZlogHRS ~ ZRi",




"ZlogHRS ~ ZE")  

ts.model <- glm(modelstructure[i], family = gaussian,  

# Extracts some mode results

n[i] <- length(resid(ts.model))

AICA[i] <- AIC(ts.model)


#Writes results to data file

x = data.frame(

modelnames, n, AICA)

write.table(x, paste(t.url, file="Results.txt"), sep="\t", quote=F)

----------------------------------------END R-CODE-----------------------------------





Lukas Indermaur, PhD student
eawag / Swiss Federal Institute of Aquatic Science and Technology ECO - Department of Aquatic Ecology
Überlandstrasse 133
CH-8600 Dübendorf

Phone: +41 (0) 71 220 38 25
Fax : +41 (0) 44 823 53 15
Email: mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu Jan 11 20:13:38 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 11 Jan 2007 - 09:30:25 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.