Re: [R] troubles with a for loop
Alas, speaking before I check the optional argumentsc'est la vie. MW On Sun, Sep 18, 2011 at 3:00 PM, Jorge I Velez jorgeivanve...@gmail.comwrote: Why wouldn't? It does for me ;-) curve(sin(x), 0, 10, col = 4, lwd = 2, las = 1) curve(cos(x), 0, 10, add = TRUE) Best, Jorge On Sun, Sep 18, 2011 at 2:07 PM, R. Michael Weylandt wrote: Furthermore, if you want to add lines to a plot, I'd advise against the curve() function, rather use lines() to add lines (or points() for, well, points) on a plot. E.g., x - seq(0,10,by=0.025) y - sin(x) y2 - cos(x) plot(x,y,type=l,col=red4,lwd=3) lines(x,y2) curve() wouldn't work for this Michael On Sun, Sep 18, 2011 at 1:23 PM, Patrick Burns wrote: Sarah has told you how to get from where you are to where you want to be. However, it is easier to start somewhere else. The more R way to do what you are doing is to use a list to put the results of your regressions into. It is then very easy to use that list, and it is easier to keep track of. The two documents mentioned in my signature may help you get up and running in R. Since you are coming from Stata, you should also look at http://r4stats.com On 18/09/2011 17:05, Francesco Sarracino wrote: Dear all, I am a stata user and I am moving my first steps in R. I am dealing with a silly issue concerning looping over variables. I read previous posts on similar topics in the R help archive, but I did not find a solution. Here is my case: I run a simple bivariate linear regression saving the output in objects called: pd.memb.0, pd.memb.1, pd.memb.2, pd.memb.3 pd.memb.0- lm(E.fs.memb ~ M.fs.memb, data, subset = (tag2 == 1)) pd.memb.1- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb= quantile(M.fs.memb, .25) tag2 == 1)) pd.memb.2- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .25) M.fs.memb= quantile(M.fs.memb, .75) tag2 == 1)) pd.memb.3- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .75) tag2 == 1)) Subsequently, I wish to plot my data superimposing a different line for each regression model. Here I am trying to apply my loop. Looking at the info I found around, the following code sound to me as correct, but I am wrong. plot(M.fs.memb , E.fs.memb) for(i = 1:3){ curve(coef(pd.memb.i)[1] + coef(pd.memb.i)[2]*x, add = T, cex = 100, col = red) } The plot is plotted, but the curves are not printed and I get the following error message: Error in coef(pd.memb.i) : error in evaluating the argument 'object' in selecting a method for function 'coef' }Error: unexpected '}' in } What am I doing wrong? Thanks a lot for your help, f. [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-help https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/**blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-help https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org 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@r-project.org 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] troubles with a for loop
Dear all, I am a stata user and I am moving my first steps in R. I am dealing with a silly issue concerning looping over variables. I read previous posts on similar topics in the R help archive, but I did not find a solution. Here is my case: I run a simple bivariate linear regression saving the output in objects called: pd.memb.0, pd.memb.1, pd.memb.2, pd.memb.3 pd.memb.0 - lm(E.fs.memb ~ M.fs.memb, data, subset = (tag2 == 1)) pd.memb.1 - lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb = quantile(M.fs.memb, .25) tag2 == 1)) pd.memb.2 - lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .25) M.fs.memb = quantile(M.fs.memb, .75) tag2 == 1)) pd.memb.3 - lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .75) tag2 == 1)) Subsequently, I wish to plot my data superimposing a different line for each regression model. Here I am trying to apply my loop. Looking at the info I found around, the following code sound to me as correct, but I am wrong. plot(M.fs.memb , E.fs.memb) for(i = 1:3){ curve(coef(pd.memb.i)[1] + coef(pd.memb.i)[2]*x, add = T, cex = 100, col = red) } The plot is plotted, but the curves are not printed and I get the following error message: Error in coef(pd.memb.i) : error in evaluating the argument 'object' in selecting a method for function 'coef' }Error: unexpected '}' in } What am I doing wrong? Thanks a lot for your help, f. [[alternative HTML version deleted]] __ R-help@r-project.org 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] troubles with a for loop
You don't have a variable named pd.memb.i Instead, you need something like: get(paste(pd.memb., i, sep=)) The error message you're getting, though, is because your loop syntax is wrong: for(i in 1:3) { do something } Sarah On Sun, Sep 18, 2011 at 12:05 PM, Francesco Sarracino f.sarrac...@gmail.com wrote: Dear all, I am a stata user and I am moving my first steps in R. I am dealing with a silly issue concerning looping over variables. I read previous posts on similar topics in the R help archive, but I did not find a solution. Here is my case: I run a simple bivariate linear regression saving the output in objects called: pd.memb.0, pd.memb.1, pd.memb.2, pd.memb.3 pd.memb.0 - lm(E.fs.memb ~ M.fs.memb, data, subset = (tag2 == 1)) pd.memb.1 - lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb = quantile(M.fs.memb, .25) tag2 == 1)) pd.memb.2 - lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .25) M.fs.memb = quantile(M.fs.memb, .75) tag2 == 1)) pd.memb.3 - lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .75) tag2 == 1)) Subsequently, I wish to plot my data superimposing a different line for each regression model. Here I am trying to apply my loop. Looking at the info I found around, the following code sound to me as correct, but I am wrong. plot(M.fs.memb , E.fs.memb) for(i = 1:3){ curve(coef(pd.memb.i)[1] + coef(pd.memb.i)[2]*x, add = T, cex = 100, col = red) } The plot is plotted, but the curves are not printed and I get the following error message: Error in coef(pd.memb.i) : error in evaluating the argument 'object' in selecting a method for function 'coef' }Error: unexpected '}' in } What am I doing wrong? Thanks a lot for your help, f. [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Sarah Goslee http://www.functionaldiversity.org __ R-help@r-project.org 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] troubles with a for loop
Sarah has told you how to get from where you are to where you want to be. However, it is easier to start somewhere else. The more R way to do what you are doing is to use a list to put the results of your regressions into. It is then very easy to use that list, and it is easier to keep track of. The two documents mentioned in my signature may help you get up and running in R. Since you are coming from Stata, you should also look at http://r4stats.com On 18/09/2011 17:05, Francesco Sarracino wrote: Dear all, I am a stata user and I am moving my first steps in R. I am dealing with a silly issue concerning looping over variables. I read previous posts on similar topics in the R help archive, but I did not find a solution. Here is my case: I run a simple bivariate linear regression saving the output in objects called: pd.memb.0, pd.memb.1, pd.memb.2, pd.memb.3 pd.memb.0- lm(E.fs.memb ~ M.fs.memb, data, subset = (tag2 == 1)) pd.memb.1- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb= quantile(M.fs.memb, .25) tag2 == 1)) pd.memb.2- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .25) M.fs.memb= quantile(M.fs.memb, .75) tag2 == 1)) pd.memb.3- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .75) tag2 == 1)) Subsequently, I wish to plot my data superimposing a different line for each regression model. Here I am trying to apply my loop. Looking at the info I found around, the following code sound to me as correct, but I am wrong. plot(M.fs.memb , E.fs.memb) for(i = 1:3){ curve(coef(pd.memb.i)[1] + coef(pd.memb.i)[2]*x, add = T, cex = 100, col = red) } The plot is plotted, but the curves are not printed and I get the following error message: Error in coef(pd.memb.i) : error in evaluating the argument 'object' in selecting a method for function 'coef' }Error: unexpected '}' in } What am I doing wrong? Thanks a lot for your help, f. [[alternative HTML version deleted]] __ R-help@r-project.org 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. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ R-help@r-project.org 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] troubles with a for loop
Furthermore, if you want to add lines to a plot, I'd advise against the curve() function, rather use lines() to add lines (or points() for, well, points) on a plot. E.g., x - seq(0,10,by=0.025) y - sin(x) y2 - cos(x) plot(x,y,type=l,col=red4,lwd=3) lines(x,y2) curve() wouldn't work for this Michael On Sun, Sep 18, 2011 at 1:23 PM, Patrick Burns pbu...@pburns.seanet.comwrote: Sarah has told you how to get from where you are to where you want to be. However, it is easier to start somewhere else. The more R way to do what you are doing is to use a list to put the results of your regressions into. It is then very easy to use that list, and it is easier to keep track of. The two documents mentioned in my signature may help you get up and running in R. Since you are coming from Stata, you should also look at http://r4stats.com On 18/09/2011 17:05, Francesco Sarracino wrote: Dear all, I am a stata user and I am moving my first steps in R. I am dealing with a silly issue concerning looping over variables. I read previous posts on similar topics in the R help archive, but I did not find a solution. Here is my case: I run a simple bivariate linear regression saving the output in objects called: pd.memb.0, pd.memb.1, pd.memb.2, pd.memb.3 pd.memb.0- lm(E.fs.memb ~ M.fs.memb, data, subset = (tag2 == 1)) pd.memb.1- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb= quantile(M.fs.memb, .25) tag2 == 1)) pd.memb.2- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .25) M.fs.memb= quantile(M.fs.memb, .75) tag2 == 1)) pd.memb.3- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .75) tag2 == 1)) Subsequently, I wish to plot my data superimposing a different line for each regression model. Here I am trying to apply my loop. Looking at the info I found around, the following code sound to me as correct, but I am wrong. plot(M.fs.memb , E.fs.memb) for(i = 1:3){ curve(coef(pd.memb.i)[1] + coef(pd.memb.i)[2]*x, add = T, cex = 100, col = red) } The plot is plotted, but the curves are not printed and I get the following error message: Error in coef(pd.memb.i) : error in evaluating the argument 'object' in selecting a method for function 'coef' }Error: unexpected '}' in } What am I doing wrong? Thanks a lot for your help, f. [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/**blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org 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] troubles with a for loop
Why wouldn't? It does for me ;-) curve(sin(x), 0, 10, col = 4, lwd = 2, las = 1) curve(cos(x), 0, 10, add = TRUE) Best, Jorge On Sun, Sep 18, 2011 at 2:07 PM, R. Michael Weylandt wrote: Furthermore, if you want to add lines to a plot, I'd advise against the curve() function, rather use lines() to add lines (or points() for, well, points) on a plot. E.g., x - seq(0,10,by=0.025) y - sin(x) y2 - cos(x) plot(x,y,type=l,col=red4,lwd=3) lines(x,y2) curve() wouldn't work for this Michael On Sun, Sep 18, 2011 at 1:23 PM, Patrick Burns wrote: Sarah has told you how to get from where you are to where you want to be. However, it is easier to start somewhere else. The more R way to do what you are doing is to use a list to put the results of your regressions into. It is then very easy to use that list, and it is easier to keep track of. The two documents mentioned in my signature may help you get up and running in R. Since you are coming from Stata, you should also look at http://r4stats.com On 18/09/2011 17:05, Francesco Sarracino wrote: Dear all, I am a stata user and I am moving my first steps in R. I am dealing with a silly issue concerning looping over variables. I read previous posts on similar topics in the R help archive, but I did not find a solution. Here is my case: I run a simple bivariate linear regression saving the output in objects called: pd.memb.0, pd.memb.1, pd.memb.2, pd.memb.3 pd.memb.0- lm(E.fs.memb ~ M.fs.memb, data, subset = (tag2 == 1)) pd.memb.1- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb= quantile(M.fs.memb, .25) tag2 == 1)) pd.memb.2- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .25) M.fs.memb= quantile(M.fs.memb, .75) tag2 == 1)) pd.memb.3- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .75) tag2 == 1)) Subsequently, I wish to plot my data superimposing a different line for each regression model. Here I am trying to apply my loop. Looking at the info I found around, the following code sound to me as correct, but I am wrong. plot(M.fs.memb , E.fs.memb) for(i = 1:3){ curve(coef(pd.memb.i)[1] + coef(pd.memb.i)[2]*x, add = T, cex = 100, col = red) } The plot is plotted, but the curves are not printed and I get the following error message: Error in coef(pd.memb.i) : error in evaluating the argument 'object' in selecting a method for function 'coef' }Error: unexpected '}' in } What am I doing wrong? Thanks a lot for your help, f. [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-help https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/**blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-help https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org 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@r-project.org 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] troubles with a for loop
Wow, thanks a lot for your quick replies and useful comments. Indeed, Sarah's advice does the job perfectly. I wish to thank you all also for your advices on how to shift from a Stata mindset to an R one (which is currently my main obstacle). Best, f. On 18 September 2011 20:07, R. Michael Weylandt michael.weyla...@gmail.comwrote: Furthermore, if you want to add lines to a plot, I'd advise against the curve() function, rather use lines() to add lines (or points() for, well, points) on a plot. E.g., x - seq(0,10,by=0.025) y - sin(x) y2 - cos(x) plot(x,y,type=l,col=red4,lwd=3) lines(x,y2) curve() wouldn't work for this Michael On Sun, Sep 18, 2011 at 1:23 PM, Patrick Burns pbu...@pburns.seanet.comwrote: Sarah has told you how to get from where you are to where you want to be. However, it is easier to start somewhere else. The more R way to do what you are doing is to use a list to put the results of your regressions into. It is then very easy to use that list, and it is easier to keep track of. The two documents mentioned in my signature may help you get up and running in R. Since you are coming from Stata, you should also look at http://r4stats.com On 18/09/2011 17:05, Francesco Sarracino wrote: Dear all, I am a stata user and I am moving my first steps in R. I am dealing with a silly issue concerning looping over variables. I read previous posts on similar topics in the R help archive, but I did not find a solution. Here is my case: I run a simple bivariate linear regression saving the output in objects called: pd.memb.0, pd.memb.1, pd.memb.2, pd.memb.3 pd.memb.0- lm(E.fs.memb ~ M.fs.memb, data, subset = (tag2 == 1)) pd.memb.1- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb= quantile(M.fs.memb, .25) tag2 == 1)) pd.memb.2- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .25) M.fs.memb= quantile(M.fs.memb, .75) tag2 == 1)) pd.memb.3- lm(E.fs.memb ~ M.fs.memb, data, subset = (M.fs.memb quantile(M.fs.memb, .75) tag2 == 1)) Subsequently, I wish to plot my data superimposing a different line for each regression model. Here I am trying to apply my loop. Looking at the info I found around, the following code sound to me as correct, but I am wrong. plot(M.fs.memb , E.fs.memb) for(i = 1:3){ curve(coef(pd.memb.i)[1] + coef(pd.memb.i)[2]*x, add = T, cex = 100, col = red) } The plot is plotted, but the curves are not printed and I get the following error message: Error in coef(pd.memb.i) : error in evaluating the argument 'object' in selecting a method for function 'coef' }Error: unexpected '}' in } What am I doing wrong? Thanks a lot for your help, f. [[alternative HTML version deleted]] __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Patrick Burns pbu...@pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/**blog http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org 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.