Re: [R] batch job GLM calculations
Hi, Someone help-me on this task some weeks ago, and it is working find to me. I use something like: - findmodels - function(modeltype = lm, dataset, pattern) { ls - ls(.GlobalEnv, pattern=pattern) mods - ls[sapply(ls, function(x) inherits(get(x), modeltype))] if (!missing(dataset)) { data.name - function(x) as.character(x$call[[data]]) mods - mods[sapply(mods, function(x) data.name == dataset)] } models - lapply(mods, get) class(models) - c(ensemble, class(models)) models } mod_1-glm() mod_2-glm() models-NULL models- findmodels(pattern=mod_) - So you can use a for (i in 1:length(models)) to extract the statistics tha you want for each model and save it in a file (I´m saving it easily in Access using RODBC). I hope this help, Miltinho Brazil Indermaur Lukas [EMAIL PROTECTED] escreveu: 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, data=t.data) (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, data=t.data). It would considerably ease my workload if there is a solution to the indexing problem within a GLM. I appreciate any hint. Best regards Lukas p.s. 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 #global modelstructure - c( ZlogHRS ~ ZRi+ZE+ZPROX_MN+ZED+ZAlwd+ZT2+ZW+ZN+Sex+y, #biotic1 ZlogHRS ~ ZRi, #biotic2 ZlogHRS ~ ZPROX_MN, #abiotic ZlogHRS ~ ZE) ts.model - glm(modelstructure[i], family = gaussian, data=t.data) # 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 Switzerland Phone: +41 (0) 71 220 38 25 Fax : +41 (0) 44 823 53 15 Email: [EMAIL PROTECTED] www.lukasindermaur.ch __ R-help@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 and provide commented, minimal, self-contained, reproducible code. __ [[alternative HTML version deleted]] __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
[R] batch job GLM calculations
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, data=t.data) (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, data=t.data). It would considerably ease my workload if there is a solution to the indexing problem within a GLM. I appreciate any hint. Best regards Lukas p.s. 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 #global modelstructure - c( ZlogHRS ~ ZRi+ZE+ZPROX_MN+ZED+ZAlwd+ZT2+ZW+ZN+Sex+y, #biotic1 ZlogHRS ~ ZRi, #biotic2 ZlogHRS ~ ZPROX_MN, #abiotic ZlogHRS ~ ZE) ts.model - glm(modelstructure[i], family = gaussian, data=t.data) # 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 Switzerland Phone: +41 (0) 71 220 38 25 Fax: +41 (0) 44 823 53 15 Email: [EMAIL PROTECTED] www.lukasindermaur.ch __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] batch job GLM calculations
as.formula(modelstructure[i]) in the glm function Indermaur Lukas [EMAIL PROTECTED] 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, data=t.data) (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, data=t.data). It would considerably ease my workload if there is a solution to the indexing problem within a GLM. I appreciate any hint. Best regards Lukas p.s. 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 #global modelstructure - c( ZlogHRS ~ ZRi+ZE+ZPROX_MN+ZED+ZAlwd+ZT2+ZW+ZN+Sex+y, #biotic1 ZlogHRS ~ ZRi, #biotic2 ZlogHRS ~ ZPROX_MN, #abiotic ZlogHRS ~ ZE) ts.model - glm(modelstructure[i], family = gaussian, data=t.data) # 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 Switzerland Phone: +41 (0) 71 220 38 25 Fax: +41 (0) 44 823 53 15 Email: [EMAIL PROTECTED] www.lukasindermaur.ch __ R-help@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 and provide commented, minimal, self-contained, reproducible code. __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] batch job GLM calculations
From ?glm: formula: a symbolic description of the model to be fit. The details of model specification are given below. which could be clearer. It needs to be a object of type 'formula'. I believe as.formula(modelstructure[i]) will do what you want (but your posting has come out rather awkwardly laid out in double spacing). On Thu, 11 Jan 2007, Indermaur Lukas wrote: 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, data=t.data) (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, data=t.data). It would considerably ease my workload if there is a solution to the indexing problem within a GLM. I appreciate any hint. Best regards Lukas p.s. 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 #global modelstructure - c( ZlogHRS ~ ZRi+ZE+ZPROX_MN+ZED+ZAlwd+ZT2+ZW+ZN+Sex+y, #biotic1 ZlogHRS ~ ZRi, #biotic2 ZlogHRS ~ ZPROX_MN, #abiotic ZlogHRS ~ ZE) ts.model - glm(modelstructure[i], family = gaussian, data=t.data) # 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 Switzerland Phone: +41 (0) 71 220 38 25 Fax: +41 (0) 44 823 53 15 Email: [EMAIL PROTECTED] www.lukasindermaur.ch __ R-help@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 and provide commented, minimal, self-contained, reproducible code. -- Brian D. Ripley, [EMAIL PROTECTED] 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, UKFax: +44 1865 272595__ R-help@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 and provide commented, minimal, self-contained, reproducible code.