Hi So, I have a dataset and I'm trying to solve for a parameter in an equation using the dataset. Before applying more sophisticated statistical techniques, I want to tell R to solve it out for each observation. I know I have to use a loop for it (and I have done that before, but am a bit rusty), but before I do that, I'm trying to get R to solve it for just one observation, to make sure that I have that part right before applying to the rest. However, when I do it, I get 0 for an answer, which is a valid answer, but a trivial one. What am I doing wrong? (tips for the loop would also be appreciated, but this is my main concern right now.) Here's my code: f<-function(r){1-exp(-r*b)-(1/2)*(1-exp(-r*x1)+1-exp(-r*x2))} uniroot(f,c(0,1))$root r is the unknown. I've plugged in a couple numbers from my data for b, x1, and x2 such as: b=5500 x1=0.01 x2=10000 and b=198000 x1=10 x2=500000 and gotten 0 both times. Thanks in advance for your help. -- View this message in context: http://www.nabble.com/Solving-an-equation-in-R-%28and-using-a-loop%29-tp25916187p25916187.html Sent from the R help mailing list archive at Nabble.com.
On Oct 15, 2009, at 5:18 PM, leohearn wrote:> > So, I have a dataset and I'm trying to solve for a parameter in an > equation > using the dataset. Before applying more sophisticated statistical > techniques, I want to tell R to solve it out for each observation. I > know I > have to use a loop for it (and I have done that before, but am a bit > rusty), > but before I do that, I'm trying to get R to solve it for just one > observation, to make sure that I have that part right before > applying to the > rest. However, when I do it, I get 0 for an answer, which is a valid > answer, > but a trivial one. What am I doing wrong? (tips for the loop would > also be > appreciated, but this is my main concern right now.) > > Here's my code: > f<-function(r){1-exp(-r*b)-(1/2)*(1-exp(-r*x1)+1-exp(-r*x2))}What is this function supposed to represent?> uniroot(f,c(0,1))$root > > r is the unknown. I've plugged in a couple numbers from my data for > b, x1, > and x2 such as: > b=5500 > x1=0.01 > x2=10000 > and > b=198000 > x1=10 > x2=500000 > and gotten 0 both times.Apparently you do understand that at zero that function would evaluate to 1-1- (1/2)*(1-1+1-1) for any value of the parameters. If you plot that function over the domain specified, you should see why there are no other solutions. Even with the first set of parameters f(1) is [1] 2.269996e-05 # which is close but not equal to zero Sometimes the trivial answer is the _only_ answer.> > Thanks in advance for your help. > --David Winsemius, MD Heritage Laboratories West Hartford, CT
I tried :> test <- 0:10000 > test2 <- f(0:10000) > plot(test,test2) > plot(test,log(test2))and> test <- seq(0,1,by=0.00001) > test2 <- sapply(test,f) > plot(test,log(test2)) > plot(test,test2)with the values you gave. And according to this result, you did nothing wrong. Zero is the only root of the function, as it is a monotone asymptotic to 0 for x>0, and not defined for negative x. Are you sure you're looking for the root of that function? Cheers Joris On Thu, Oct 15, 2009 at 11:18 PM, leohearn <leohearn at dal.ca> wrote:> > Hi > So, I have a dataset and I'm trying to solve for a parameter in an equation > using the dataset. Before applying more sophisticated statistical > techniques, I want to tell R to solve it out for each observation. I know I > have to use a loop for it (and I have done that before, but am a bit rusty), > but before I do that, I'm trying to get R to solve it for just one > observation, to make sure that I have that part right before applying to the > rest. However, when I do it, I get 0 for an answer, which is a valid answer, > but a trivial one. What am I doing wrong? (tips for the loop would also be > appreciated, but this is my main concern right now.) > > Here's my code: > f<-function(r){1-exp(-r*b)-(1/2)*(1-exp(-r*x1)+1-exp(-r*x2))} > uniroot(f,c(0,1))$root > > r is the unknown. I've plugged in a couple numbers from my data for b, x1, > and x2 such as: > b=5500 > x1=0.01 > x2=10000 > and > b=198000 > x1=10 > x2=500000 > and gotten 0 both times. > > Thanks in advance for your help. > -- > View this message in context: http://www.nabble.com/Solving-an-equation-in-R-%28and-using-a-loop%29-tp25916187p25916187.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >
Thank you both for your help. I'll ask my advisor for help on the issue tomorrow as perhaps I've made a specification error or I'm taking the wrong approach entirely. I'm a master's student in economics; the equation is comprised of CARA utility functions(CARA utility is when u(x) = 1-exp(-rx)), and the r represents the corresponding individual's coefficient of risk aversion. Perhaps I just have a selection bias since I am choosing from observations that only have x1 and x2, when I have some observations that go all the way up to x20. (so the part of f which follows the negative sign would be (1/n)(sum(1-exp(-rx(i)). I am solving for r when that equation equals 0, so, yes, I'm looking for the root. I'll try with a few more observations that have more xs. cheers Laura leohearn wrote:> > Hi > So, I have a dataset and I'm trying to solve for a parameter in an > equation using the dataset. Before applying more sophisticated statistical > techniques, I want to tell R to solve it out for each observation. I know > I have to use a loop for it (and I have done that before, but am a bit > rusty), but before I do that, I'm trying to get R to solve it for just one > observation, to make sure that I have that part right before applying to > the rest. However, when I do it, I get 0 for an answer, which is a valid > answer, but a trivial one. What am I doing wrong? (tips for the loop would > also be appreciated, but this is my main concern right now.) > > Here's my code: > f<-function(r){1-exp(-r*b)-(1/2)*(1-exp(-r*x1)+1-exp(-r*x2))} > uniroot(f,c(0,1))$root > > r is the unknown. I've plugged in a couple numbers from my data for b, x1, > and x2 such as: > b=5500 > x1=0.01 > x2=10000 > and > b=198000 > x1=10 > x2=500000 > and gotten 0 both times. > > Thanks in advance for your help. >-- View this message in context: http://www.nabble.com/Solving-an-equation-in-R-%28and-using-a-loop%29-tp25916187p25917285.html Sent from the R help mailing list archive at Nabble.com.
Maybe Matching Threads
- Solving a simple linear equation using uniroot give error object 'x' not found
- Solving a simple linear equation using uniroot give error object 'x' not found
- Problem : solving a equation with R , fail with uniroot function
- Solving the equation using uniroot
- Solving quadratic equation in R