Sorkin, John
2020-Oct-06 15:00 UTC
[R] Solving a simple linear equation using uniroot give error object 'x' not found
Colleagues, I am trying to learn to use uniroot to solve a simple linear equation. I define the function, prove the function and a call to the function works. When I try to use uniroot to solve the equation I get an error message, Error in yfu n(x,10,20) : object 'x' not found. I hope someone can tell we how I can fix the problem if(!require(rootSolve)){install.packages("rootSolve")} library(rootSolve) # Define the function yfun <- function(x,diffMean,diffSE) ((abs(diffMean)-x)/diffSE) # Prove the function and a call to the function work yfun(10,100,10) # use uniroot to find a solution to the # function in the range -100 to 100 uniroot(yfun(x,10,20),c(-100,100)) My output:> # Define the function > yfun <- function(x,diffMean,diffSE) ((abs(diffMean)-x)/diffSE) > # Prove the function and a call to the funciton works > yfun(10,100,10)[1] 9> # use uniroot to find a solution to the > # funciton in the range -100 to 100 > uniroot(yfun(x,10,20),c(-100,100))Error in yfun(x, 10, 20) : object 'x' not found Thank you, John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) [[alternative HTML version deleted]]
Duncan Murdoch
2020-Oct-06 15:06 UTC
[R] Solving a simple linear equation using uniroot give error object 'x' not found
On 06/10/2020 11:00 a.m., Sorkin, John wrote:> Colleagues, > I am trying to learn to use uniroot to solve a simple linear equation. I define the function, prove the function and a call to the function works. When I try to use uniroot to solve the equation I get an error message, > Error in yfu n(x,10,20) : object 'x' not found. > > I hope someone can tell we how I can fix the problem > > if(!require(rootSolve)){install.packages("rootSolve")} > library(rootSolve) > > # Define the function > yfun <- function(x,diffMean,diffSE) ((abs(diffMean)-x)/diffSE) > # Prove the function and a call to the function work > yfun(10,100,10) > # use uniroot to find a solution to the > # function in the range -100 to 100 > uniroot(yfun(x,10,20),c(-100,100)) > > > My output: >> # Define the function >> yfun <- function(x,diffMean,diffSE) ((abs(diffMean)-x)/diffSE) >> # Prove the function and a call to the funciton works >> yfun(10,100,10) > [1] 9 >> # use uniroot to find a solution to the >> # funciton in the range -100 to 100 >> uniroot(yfun(x,10,20),c(-100,100)) > Error in yfun(x, 10, 20) : object 'x' not foundThe problem is that uniroot() wants a function as first argument, and yfun(x,10,20) is not a function, it's the value of a function. You can do it with uniroot(function(x) yfun(x,10,20),c(-100,100)) which creates the anonymous function function(x) yfun(x,10,20) and passes that to uniroot(). Duncan Murdoch> > Thank you, > John > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > [[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. >
Jeff Newmiller
2020-Oct-06 15:28 UTC
[R] Solving a simple linear equation using uniroot give error object 'x' not found
Have you looked at the examples in ?uniroot? Don't call yfun... let uniroot do that. On October 6, 2020 8:00:59 AM PDT, "Sorkin, John" <jsorkin at som.umaryland.edu> wrote:>Colleagues, >I am trying to learn to use uniroot to solve a simple linear equation. >I define the function, prove the function and a call to the function >works. When I try to use uniroot to solve the equation I get an error >message, >Error in yfu n(x,10,20) : object 'x' not found. > >I hope someone can tell we how I can fix the problem > > if(!require(rootSolve)){install.packages("rootSolve")} > library(rootSolve) > > # Define the function > yfun <- function(x,diffMean,diffSE) ((abs(diffMean)-x)/diffSE) > # Prove the function and a call to the function work > yfun(10,100,10) > # use uniroot to find a solution to the > # function in the range -100 to 100 > uniroot(yfun(x,10,20),c(-100,100)) > > >My output: >> # Define the function >> yfun <- function(x,diffMean,diffSE) ((abs(diffMean)-x)/diffSE) >> # Prove the function and a call to the funciton works >> yfun(10,100,10) >[1] 9 >> # use uniroot to find a solution to the >> # funciton in the range -100 to 100 >> uniroot(yfun(x,10,20),c(-100,100)) >Error in yfun(x, 10, 20) : object 'x' not found > >Thank you, >John > > >John David Sorkin M.D., Ph.D. >Professor of Medicine >Chief, Biostatistics and Informatics >University of Maryland School of Medicine Division of Gerontology and >Geriatric Medicine >Baltimore VA Medical Center >10 North Greene Street >GRECC (BT/18/GR) >Baltimore, MD 21201-1524 >(Phone) 410-605-7119 >(Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > [[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.-- Sent from my phone. Please excuse my brevity.
Jeff Newmiller
2020-Oct-06 16:15 UTC
[R] Solving a simple linear equation using uniroot give error object 'x' not found
... or uniroot( yfun, c( -100, 100 ), diffMean=10, diffSE=20 ) On October 6, 2020 8:06:35 AM PDT, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:>On 06/10/2020 11:00 a.m., Sorkin, John wrote: >> Colleagues, >> I am trying to learn to use uniroot to solve a simple linear >equation. I define the function, prove the function and a call to the >function works. When I try to use uniroot to solve the equation I get >an error message, >> Error in yfu n(x,10,20) : object 'x' not found. >> >> I hope someone can tell we how I can fix the problem >> >> if(!require(rootSolve)){install.packages("rootSolve")} >> library(rootSolve) >> >> # Define the function >> yfun <- function(x,diffMean,diffSE) ((abs(diffMean)-x)/diffSE) >> # Prove the function and a call to the function work >> yfun(10,100,10) >> # use uniroot to find a solution to the >> # function in the range -100 to 100 >> uniroot(yfun(x,10,20),c(-100,100)) >> >> >> My output: >>> # Define the function >>> yfun <- function(x,diffMean,diffSE) ((abs(diffMean)-x)/diffSE) >>> # Prove the function and a call to the funciton works >>> yfun(10,100,10) >> [1] 9 >>> # use uniroot to find a solution to the >>> # funciton in the range -100 to 100 >>> uniroot(yfun(x,10,20),c(-100,100)) >> Error in yfun(x, 10, 20) : object 'x' not found > >The problem is that uniroot() wants a function as first argument, and >yfun(x,10,20) is not a function, it's the value of a function. You can > >do it with > > uniroot(function(x) yfun(x,10,20),c(-100,100)) > >which creates the anonymous function > > function(x) yfun(x,10,20) > >and passes that to uniroot(). > >Duncan Murdoch >> >> Thank you, >> John >> >> >> John David Sorkin M.D., Ph.D. >> Professor of Medicine >> Chief, Biostatistics and Informatics >> University of Maryland School of Medicine Division of Gerontology and >Geriatric Medicine >> Baltimore VA Medical Center >> 10 North Greene Street >> GRECC (BT/18/GR) >> Baltimore, MD 21201-1524 >> (Phone) 410-605-7119 >> (Fax) 410-605-7913 (Please call phone number above prior to faxing) >> >> >> [[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.-- Sent from my phone. Please excuse my brevity.
Rolf Turner
2020-Oct-06 21:30 UTC
[R] Solving a simple linear equation using uniroot give error object 'x' not found
On Tue, 6 Oct 2020 15:00:59 +0000 "Sorkin, John" <jsorkin at som.umaryland.edu> wrote:> Colleagues, > I am trying to learn to use uniroot to solve a simple linear > equation. I define the function, prove the function and a call to the > function works. When I try to use uniroot to solve the equation I get > an error message, Error in yfu n(x,10,20) : object 'x' not found. > > I hope someone can tell we how I can fix the problem > > if(!require(rootSolve)){install.packages("rootSolve")} > library(rootSolve)Others have told you how to solve your problem. I am curious to know why on earth you are loading the rootSolve package. The uniroot() function is in package "stats" which is automatically available. <SNIP> cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
Maybe Matching Threads
- Solving a simple linear equation using uniroot give error object 'x' not found
- Problem : solving a equation with R , fail with uniroot function
- How do I modify uniroot function to return .0001 if error ?
- What does uniroot return when an error occurs
- uniroot speed and vectorization?