Hi, Using the example data from library(gvlma) library(gvlma) data(CarMileageData) CarMileageNew <- CarMileageData[,c(5,6,3)] ?lst1 <- list() ?y <- c("NumGallons", "NumDaysBetw") ?for(i in seq_along(y)){ ?lst1[[i]] <- gvlma(lm(get(y[i])~MilesLastFill,data=CarMileageNew)) ?lst1} pdf("gvlmaplot.pdf") ?lapply(lst1,plot) dev.off() You could also use ?lapply(). A.K. Hi I have a spread sheet with a column Samples (column1) and then 34 more columns with different concentrations of fatty acids per sample. Im trying to run the same function 34 times. In this case (the first of 34), I have a fatty acid called C14.0 (column 2). I'm a newbie with R so I spent the last 4 days looking for a way of doing it (without running the same function 34 times with a different fatty acid each time). I saw that people do similar things with loops but I cannot get them to work. I have tried the script below but it does not work. y<-c("C14.0","C15.0","C16.0") for (i in y) { FA.ml=lm(i~Sample,data=FA) gvlmaFA<-gvlma(FA.ml) gvlmaFA } I really appreciate if someone can give me a hand with that. I know would have been finished if I had typed the 34 fatty acids but I want to learn how to do it with loops. Cheers
Thank you Arun for your quick answer. Ill try it and ill let you know. Cheers On 21/04/2014, at 11:30 PM, "arun kirshna [via R]" <ml-node+s789695n4689190h10@n4.nabble.com> wrote:> Hi, > > Using the example data from library(gvlma) > > library(gvlma) > data(CarMileageData) > CarMileageNew <- CarMileageData[,c(5,6,3)] > lst1 <- list() > y <- c("NumGallons", "NumDaysBetw") > for(i in seq_along(y)){ > lst1[[i]] <- gvlma(lm(get(y[i])~MilesLastFill,data=CarMileageNew)) > lst1} > pdf("gvlmaplot.pdf") > lapply(lst1,plot) > dev.off() > > > You could also use ?lapply(). > > > A.K. > > > > Hi > I have a spread sheet with a column Samples (column1) and then 34 more columns with different concentrations of fatty acids per sample. Im trying to run the same function 34 times. In this case (the first of 34), I have a fatty acid called C14.0 (column 2). I'm a newbie with R so I spent the last 4 days looking for a way of doing it (without running the same function 34 times with a different fatty acid each time). I saw that people do similar things with loops but I cannot get them to work. > I have tried the script below but it does not work. > > y<-c("C14.0","C15.0","C16.0") > for (i in y) { > FA.ml=lm(i~Sample,data=FA) > gvlmaFA<-gvlma(FA.ml) > gvlmaFA > } > > > I really appreciate if someone can give me a hand with that. I know would have been finished if I had typed the 34 fatty acids but I want to learn how to do it with loops. > Cheers > > > ______________________________________________ > [hidden email] 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. > > > If you reply to this email, your message will be added to the discussion below: > http://r.789695.n4.nabble.com/Loops-run-the-same-function-per-different-columns-tp4689171p4689190.html > To unsubscribe from Loops (run the same function per different columns), click here. > NAML-- View this message in context: http://r.789695.n4.nabble.com/Loops-run-the-same-function-per-different-columns-tp4689171p4689228.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
Hi Arun, Your script works but it does not do what I was after. To be a bit more specific, this the table FA in which Im working on ( but the original one has 34 fatty acids instead of 3: C14.0, C15.0, and C15.1). SampleC14:0C15:0C15:1 L1 Control0.4565091920.4695626870.774909216 L1 Control0.5139896840.5279580260.732083085 L1 Control0.5558944960.5023896990.706407924 BBM Control0.4183927810.3851193291.318261983 BBM Control0.4058262920.3685645141.114889958 BBM Control0.3986339680.3918514931.238411437 L1 Ash0.5045280780.4791255770.793236101 L1 Ash0.5486679970.5175339220.632962545 L1 Ash0.4992376450.4906198580.74858627 BBM Ash0.3805822440.3800515350.996870831 BBM Ash0.3956179430.3844982160.963780759 BBM Ash0.3890271150.3708154740.923329859 I just want to run the following script but with C15.0, C15.1 and the other 32 so I can quickly scroll up and down to see who does not meet the assumptions. FA.ml=lm(C14.0~Sample,data=FA) gvlmaFA<-gvlma(FA.ml) gvlmaFA This is the result when I run the script Call: lm(formula = C14.0 ~ Sample, data = FA) Coefficients: (Intercept) SampleBBM Control SampleL1 Ash SampleL1 Control 0.38841 0.01921 0.12907 0.12039 ASSESSMENT OF THE LINEAR MODEL ASSUMPTIONS USING THE GLOBAL TEST ON 4 DEGREES-OF-FREEDOM: Level of Significance = 0.05 Call: gvlma(x = FA.ml) Value p-value Decision Global Stat 4.757e+00 0.31312 Assumptions acceptable. Skewness 1.944e-02 0.88911 Assumptions acceptable. Kurtosis 1.462e-01 0.70219 Assumptions acceptable. Link Function 3.682e-16 1.00000 Assumptions acceptable. Heteroscedasticity 4.592e+00 0.03213 Assumptions NOT satisfied! I really appreciate if you can help me with this issue. This would be really useful for me since I have large tables of data. Cheers From: "arun kirshna [via R]" <ml-node+s789695n4689190h10@n4.nabble.com> Date: Mon, 21 Apr 2014 06:29:10 -0700 (PDT) To: Carlos <carlosalvarezroa@hotmail.com> Subject: Re: Loops (run the same function per different columns) Hi, Using the example data from library(gvlma) library(gvlma) data(CarMileageData) CarMileageNew <- CarMileageData[,c(5,6,3)] lst1 <- list() y <- c("NumGallons", "NumDaysBetw") for(i in seq_along(y)){ lst1[[i]] <- gvlma(lm(get(y[i])~MilesLastFill,data=CarMileageNew)) lst1} pdf("gvlmaplot.pdf") lapply(lst1,plot) dev.off() You could also use ?lapply(). A.K. Hi I have a spread sheet with a column Samples (column1) and then 34 more columns with different concentrations of fatty acids per sample. Im trying to run the same function 34 times. In this case (the first of 34), I have a fatty acid called C14.0 (column 2). I'm a newbie with R so I spent the last 4 days looking for a way of doing it (without running the same function 34 times with a different fatty acid each time). I saw that people do similar things with loops but I cannot get them to work. I have tried the script below but it does not work. y<-c("C14.0","C15.0","C16.0") for (i in y) { FA.ml=lm(i~Sample,data=FA) gvlmaFA<-gvlma(FA.ml) gvlmaFA } I really appreciate if someone can give me a hand with that. I know would have been finished if I had typed the 34 fatty acids but I want to learn how to do it with loops. Cheers ______________________________________________ [hidden email] </user/SendEmail.jtp?type=node&node=4689190&i=0> 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. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/Loops-run-the-same-function-per-different-colu mns-tp4689171p4689190.html To unsubscribe from Loops (run the same function per different columns), click here <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by _code&node=4689171&code=Y2FybG9zYWx2YXJlenJvYUBob3RtYWlsLmNvbXw0Njg5MTcxfDIx MjE0NTUw> . NAML <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&i d=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamesp ace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNa mespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails% 21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> -- View this message in context: http://r.789695.n4.nabble.com/Loops-run-the-same-function-per-different-columns-tp4689171p4689350.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
HI, I guess you got an output like this using my script: ##Please use ?dput() to show the example data. FA <- structure(list(Sample = c("L1 Control", "L1 Control", "L1 Control", "BBM Control", "BBM Control", "BBM Control", "L1 Ash", "L1 Ash", "L1 Ash", "BBM Ash", "BBM Ash", "BBM Ash"), C14.0 = c(0.456509192, 0.513989684, 0.555894496, 0.418392781, 0.405826292, 0.398633968, 0.504528078, 0.548667997, 0.499237645, 0.380582244, 0.395617943, 0.389027115), C15.0 = c(0.469562687, 0.527958026, 0.502389699, 0.385119329, 0.368564514, 0.391851493, 0.479125577, 0.517533922, 0.490619858, 0.380051535, 0.384498216, 0.370815474), C15.1 = c(0.774909216, 0.732083085, 0.706407924, 1.318261983, 1.114889958, 1.238411437, 0.793236101, 0.632962545, 0.74858627, 0.996870831, 0.963780759, 0.923329859)), .Names = c("Sample", "C14.0", "C15.0", "C15.1" ), class = "data.frame", row.names = c(NA, -12L)) library(gvlma) y <- names(FA)[-1] ?y #[1] "C14.0" "C15.0" "C15.1" lst1 <- setNames(vector("list", length(y)),y) ?for(i in y){ ?lst1[[i]] <- gvlma(lm(get(i)~Sample,data=FA)) ?lst1}? lst1[[1]] # #Call: #lm(formula = get(y[i]) ~ Sample, data = FA) #--------------------------------------- But, you wanted to show each of the list output as in gvlmaFA. gvlmaFA <- gvlma(lm(C14.0~Sample,data=FA)) In my previous script, I didn't name the list.? Here, by setting the names as in "y", it could be easier.? I guess you wanted to reflect that in the model formula as well. lst2 <- setNames(vector("list", length(y)), y) for(names in y){ lst2[[names]] <- eval(bquote(gvlma(lm(.(names1)~ Sample, data=FA)), list(names1=as.name(names)))) lst2} identical(gvlmaFA, lst2[[1]]) #[1] TRUE A.K. Hi Arun, Your script works but it does not do what I was after. To be a bit more specific,? this the table FA in which Im working on ( but the original one has 34 fatty acids instead of 3: C14.0, C15.0, and C15.1). Sample ??? C14:0 ??? C15:0 ??? C15:1 L1 Control ??? 0.456509192 ??? 0.469562687 ??? 0.774909216 L1 Control ??? 0.513989684 ??? 0.527958026 ??? 0.732083085 L1 Control ??? 0.555894496 ??? 0.502389699 ??? 0.706407924 BBM Control ??? 0.418392781 ??? 0.385119329 ??? 1.318261983 BBM Control ??? 0.405826292 ??? 0.368564514 ??? 1.114889958 BBM Control ??? 0.398633968 ??? 0.391851493 ??? 1.238411437 L1 Ash ??? 0.504528078 ??? 0.479125577 ??? 0.793236101 L1 Ash ??? 0.548667997 ??? 0.517533922 ??? 0.632962545 L1 Ash ??? 0.499237645 ??? 0.490619858 ??? 0.74858627 BBM Ash ??? 0.380582244 ??? 0.380051535 ??? 0.996870831 BBM Ash ??? 0.395617943 ??? 0.384498216 ??? 0.963780759 BBM Ash ??? 0.389027115 ??? 0.370815474 ??? 0.923329859 I just want to run the following script but with C15.0, C15.1 and the other 32 so I can quickly scroll up and down to see who does not meet the assumptions. FA.ml=lm(C14.0~Sample,data=FA) gvlmaFA<-gvlma(FA.ml) gvlmaFA This is the result when I run the script Call: lm(formula = C14.0 ~ Sample, data = FA) Coefficients: ????? (Intercept)? SampleBBM Control?????? SampleL1 Ash?? SampleL1 Control? ????????? 0.38841??????????? 0.01921??????????? 0.12907??????????? 0.12039? ASSESSMENT OF THE LINEAR MODEL ASSUMPTIONS USING THE GLOBAL TEST ON 4 DEGREES-OF-FREEDOM: Level of Significance =? 0.05 Call: ?gvlma(x = FA.ml) ?????????????????????? Value p-value?????????????????? Decision Global Stat??????? 4.757e+00 0.31312??? Assumptions acceptable. Skewness?????????? 1.944e-02 0.88911??? Assumptions acceptable. Kurtosis?????????? 1.462e-01 0.70219??? Assumptions acceptable. Link Function????? 3.682e-16 1.00000??? Assumptions acceptable. Heteroscedasticity 4.592e+00 0.03213 Assumptions NOT satisfied! I really appreciate if you can help me with this issue. This would be really useful for me since I have large tables of data. Cheers On Monday, April 21, 2014 9:19 AM, arun <smartpink111 at yahoo.com> wrote: Hi, Using the example data from library(gvlma) library(gvlma) data(CarMileageData) CarMileageNew <- CarMileageData[,c(5,6,3)] ?lst1 <- list() ?y <- c("NumGallons", "NumDaysBetw") ?for(i in seq_along(y)){ ?lst1[[i]] <- gvlma(lm(get(y[i])~MilesLastFill,data=CarMileageNew)) ?lst1} pdf("gvlmaplot.pdf") ?lapply(lst1,plot) dev.off() You could also use ?lapply(). A.K. Hi I have a spread sheet with a column Samples (column1) and then 34 more columns with different concentrations of fatty acids per sample. Im trying to run the same function 34 times. In this case (the first of 34), I have a fatty acid called C14.0 (column 2). I'm a newbie with R so I spent the last 4 days looking for a way of doing it (without running the same function 34 times with a different fatty acid each time). I saw that people do similar things with loops but I cannot get them to work. I have tried the script below but it does not work. y<-c("C14.0","C15.0","C16.0") for (i in y) { FA.ml=lm(i~Sample,data=FA) gvlmaFA<-gvlma(FA.ml) gvlmaFA } I really appreciate if someone can give me a hand with that. I know would have been finished if I had typed the 34 fatty acids but I want to learn how to do it with loops. Cheers