Hello I was trying to set up a function() that allows easely to calculate regressions for severel dependent variables with the same independent variables. My function looked like this but was turned down by an error (Im quiet new to R Studio). So here is my solution: b CO2 logTrop_Aerosol lm(b ~ CO2 + logTrop_Aerosol ) Trend <- function(x,CO2,logTrop_Aerosol) { lm x CO2 + logTrop_Aerosol} b is a vector containing 400 values of heat content CO2 also contains 400 values as well as logTropAerosol my idea would be that I simply can replace x (heat content) by other vectors containing heat content. The error I got was the following: Error: unexpected symbol in "Trend <- function(x,y,z) { lm x" Thanks a lot for the help! Best Matthias [[alternative HTML version deleted]]
You may have additional scoping problems depending on where you various variables exist, but your Trend function syntax is incorrect. You need parentheses around your arguments to the lm call, and you left out the '~' in your formula. In addition, don't fool yourself by your use of the names CO2 and logTrop_Aerosol as the function arguments. Those arguments don't refer to variables in the global environment. Function parameters have local scope in the function. Trend <- function(x,CO2,logTrop_Aerosol) { lm(x ~ CO2 + logTrop_Aerosol)} Dan Daniel Nordlund, PhD Research and Data Analysis Division Services & Enterprise Support Administration Washington State Department of Social and Health Services -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Matthias Worni Sent: Thursday, November 12, 2015 8:28 AM To: r-help at r-project.org Subject: [R] Compute Regressions with R-Function Hello I was trying to set up a function() that allows easely to calculate regressions for severel dependent variables with the same independent variables. My function looked like this but was turned down by an error (Im quiet new to R Studio). So here is my solution: b CO2 logTrop_Aerosol lm(b ~ CO2 + logTrop_Aerosol ) Trend <- function(x,CO2,logTrop_Aerosol) { lm x CO2 + logTrop_Aerosol} b is a vector containing 400 values of heat content CO2 also contains 400 values as well as logTropAerosol my idea would be that I simply can replace x (heat content) by other vectors containing heat content. The error I got was the following: Error: unexpected symbol in "Trend <- function(x,y,z) { lm x" Thanks a lot for the help! Best Matthias [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
In-line On 12/11/2015 16:27, Matthias Worni wrote:> Hello > > I was trying to set up a function() that allows easely to calculate > regressions for severel dependent variables with the same independent > variables. > > My function looked like this but was turned down by an error (Im quiet new > to R Studio). So here is my solution: > > b > CO2 > logTrop_Aerosol > lm(b ~ CO2 + logTrop_Aerosol ) > > Trend <- function(x,CO2,logTrop_Aerosol) { lm x CO2 + logTrop_Aerosol}Since you posted in HTML it is hard to be sure but I would have thought you have left out the parentheses and the ~ symbol.> > b is a vector containing 400 values of heat content > CO2 also contains 400 values as well as logTropAerosol > > my idea would be that I simply can replace x (heat content) by other > vectors containing heat content. > > The error I got was the following: > > Error: unexpected symbol in "Trend <- function(x,y,z) { lm x" > > Thanks a lot for the help! > > Best Matthias > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >-- Michael http://www.dewey.myzen.co.uk/home.html
Your email is severely damaged because you sent it using HTML format. Please adjust the settings in your email software to use only plain text when sending to this list so we can see what you see. While you are at it you should read about making your examples reproducible [1]... it may seem like a pain but you are likely to get better assistance if you make the extra effort before pressing the send button. (Also, RStudio is a fine way to use R, but many people here use the R interpreter just fine without it so ask all you like about R here but don't assume we are all looking at the same screen you are. If you find you need to refer to RStudio to ask your question then you may be going off-topic. Your current question seems on topic though.) I don't see a problem with your approach such as I can guess where you are going with it, but your code is messed up and your error does not look like it goes with that code so I have no idea where your actual problem is. [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. On November 12, 2015 8:27:58 AM PST, Matthias Worni <wornimatthias at gmail.com> wrote:>Hello > >I was trying to set up a function() that allows easely to calculate >regressions for severel dependent variables with the same independent >variables. > >My function looked like this but was turned down by an error (Im quiet >new >to R Studio). So here is my solution: > >b >CO2 >logTrop_Aerosol >lm(b ~ CO2 + logTrop_Aerosol ) > >Trend <- function(x,CO2,logTrop_Aerosol) { lm x CO2 + >logTrop_Aerosol} > >b is a vector containing 400 values of heat content >CO2 also contains 400 values as well as logTropAerosol > >my idea would be that I simply can replace x (heat content) by other >vectors containing heat content. > >The error I got was the following: > >Error: unexpected symbol in "Trend <- function(x,y,z) { lm x" > >Thanks a lot for the help! > >Best Matthias > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
No function is necessary for multiple lhs's. lm() allows a matrix as it's lhs and will fit a separate regression for each column of the matrix. Please read ?lm carefully for details. For that matter, please read the linked Help pages and/or spend some time with a tutorial (there are many on the web) to learn proper linear models syntax. There are subtleties involved (formulas have environments, use of ".", use of I(), etc.) that require some effort to learn. It may help you avoid further difficulties down the road. Cheers, Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Thu, Nov 12, 2015 at 9:37 AM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:> Your email is severely damaged because you sent it using HTML format. Please adjust the settings in your email software to use only plain text when sending to this list so we can see what you see. > > While you are at it you should read about making your examples reproducible [1]... it may seem like a pain but you are likely to get better assistance if you make the extra effort before pressing the send button. > > (Also, RStudio is a fine way to use R, but many people here use the R interpreter just fine without it so ask all you like about R here but don't assume we are all looking at the same screen you are. If you find you need to refer to RStudio to ask your question then you may be going off-topic. Your current question seems on topic though.) > > I don't see a problem with your approach such as I can guess where you are going with it, but your code is messed up and your error does not look like it goes with that code so I have no idea where your actual problem is. > > [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example > --------------------------------------------------------------------------- > Jeff Newmiller The ..... ..... Go Live... > DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... > Live: OO#.. Dead: OO#.. Playing > Research Engineer (Solar/Batteries O.O#. #.O#. with > /Software/Embedded Controllers) .OO#. .OO#. rocks...1k > --------------------------------------------------------------------------- > Sent from my phone. Please excuse my brevity. > > On November 12, 2015 8:27:58 AM PST, Matthias Worni <wornimatthias at gmail.com> wrote: >>Hello >> >>I was trying to set up a function() that allows easely to calculate >>regressions for severel dependent variables with the same independent >>variables. >> >>My function looked like this but was turned down by an error (Im quiet >>new >>to R Studio). So here is my solution: >> >>b >>CO2 >>logTrop_Aerosol >>lm(b ~ CO2 + logTrop_Aerosol ) >> >>Trend <- function(x,CO2,logTrop_Aerosol) { lm x CO2 + >>logTrop_Aerosol} >> >>b is a vector containing 400 values of heat content >>CO2 also contains 400 values as well as logTropAerosol >> >>my idea would be that I simply can replace x (heat content) by other >>vectors containing heat content. >> >>The error I got was the following: >> >>Error: unexpected symbol in "Trend <- function(x,y,z) { lm x" >> >>Thanks a lot for the help! >> >>Best Matthias >> >> [[alternative HTML version deleted]] >> >>______________________________________________ >>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.