Re: [R] help about solving two equations
Ravi Varadhan wrote: require(BB) fn - function(x, s){ f - rep(NA, length(x)) f[1] - digamma(x[1]) - digamma(x[1]+x[2]) - s[1] f[2] - digamma(x[2]) - digamma(x[1]+x[2]) - s[2] f } nr - 10 smat - matrix(runif(2*nr, -3, -1), nr, 2) soln - matrix(NA, nr, 2) for (i in 1:nr) { ans - dfsane(par=c(1,1), fn=fn, s=smat[i, ], control=list(trace=FALSE)) soln[i, ] - ans$par } soln # your solution As I mentioned in a previous post to the original question, it is necessary to record if an algorithm actually found a solution. I would also advise you to try an alternative to BB; it's a bit quicker in your case and seems to fail less frequently. Like this: library(nleqslv) library(BB) fn - function(x, s){ f - rep(NA, length(x)) f[1] - digamma(x[1]) - digamma(x[1]+x[2]) - s[1] f[2] - digamma(x[2]) - digamma(x[1]+x[2]) - s[2] f } s - c(-2,-4) xstart - c(1,1) Krep - 100 smat - matrix(runif(2*Krep, -3, -1), Krep, 2) sobb - matrix(NA, Krep, 2) sonl - matrix(NA, Krep, 2) system.time( { noncvg - 0 for(k in seq(Krep)) { ans - nleqslv(xstart,fn, s=smat[k,],method=Newton) if(ans$termcd1){noncvg-noncvg+1;sonl[k,] - c(NA,NA)} else sonl[k,] - ans$x } msg - sprintf(Non convergence %d == %.2f%%\n, noncvg, noncvg/Krep*100) print(msg) }) system.time( { noncvg - 0 for(k in seq(Krep)) { ans - dfsane(par=xstart, fn=fn, s=smat[k,],control=list(trace=FALSE)) if(ans$convergence0){noncvg-noncvg+1;sobb[k,] - c(NA,NA)} else sobb[k,] - ans$par } msg - sprintf(Non convergence %d == %.2f%%\n, noncvg, noncvg/Krep*100) print(msg) }) sonl sobb Berend -- View this message in context: http://n4.nabble.com/help-about-solving-two-equations-tp1588313p1591524.html Sent from the R help mailing list archive at Nabble.com. __ 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] help about solving two equations
Here you go: require(BB) fn - function(x, s){ f - rep(NA, length(x)) f[1] - digamma(x[1]) - digamma(x[1]+x[2]) - s[1] f[2] - digamma(x[2]) - digamma(x[1]+x[2]) - s[2] f } nr - 10 smat - matrix(runif(2*nr, -3, -1), nr, 2) soln - matrix(NA, nr, 2) for (i in 1:nr) { ans - dfsane(par=c(1,1), fn=fn, s=smat[i, ], control=list(trace=FALSE)) soln[i, ] - ans$par } soln # your solution Hope this helps, Ravi. -Original Message- From: Shaoqiong Zhao [mailto:zh...@uwm.edu] Sent: Friday, March 12, 2010 5:20 PM To: Ravi Varadhan Cc: r-help@r-project.org Subject: Re: [R] help about solving two equations Hello Professor Ravi, I installed the BB package. It is so useful. Thanks a lot for developing this package for solving nonloinear equations. I tried couple of different ways to do the loop, but still ccant work it out. Now I have s, which is a 1000*2 matrix, I want to solve p,q for each row of s, I use the following loop but it still give me one value for p,q for (i in 1:1000) { fn - function(x, s){ f - rep(NA, length(x)) f[1] - digamma(x[1]) - digamma(x[1]+x[2]) - s[i,1] f[2] - digamma(x[2]) - digamma(x[1]+x[2]) - s[i,2] f } s - matrix( ans - dfsane(par=c(1,1), fn=fn, s=s) } ans$par I still only get one value for p,q. I tried couple of different ways to do the loop, still didnt get any results. Sorry to bother you again. Best, Annie - Original Message - From: Ravi Varadhan rvarad...@jhmi.edu To: Shaoqiong Zhao zh...@uwm.edu Cc: r-help@r-project.org Sent: Thursday, March 11, 2010 8:02:43 PM GMT -06:00 US/Canada Central Subject: Re: [R] help about solving two equations You have to install it from CRAN before you can load it into your session. Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu - Original Message - From: Shaoqiong Zhao zh...@uwm.edu Date: Thursday, March 11, 2010 9:00 pm Subject: Re: [R] help about solving two equations To: Ravi Varadhan rvarad...@jhmi.edu Cc: r-help@r-project.org Hello Professor Ravi, I tried to load BB into R, but I got the following message: library(BB) Error in library(BB) : there is no package called 'BB' library(BB) Error in library(BB) : there is no package called 'BB' Can you tell me why? Thanks a lot. Annie - Original Message - From: Ravi Varadhan rvarad...@jhmi.edu To: Shaoqiong Zhao zh...@uwm.edu Cc: r-help@r-project.org Sent: Wednesday, March 10, 2010 9:03:25 PM GMT -06:00 US/Canada Central Subject: Re: [R] help about solving two equations Here is how you can solve: fn - function(x, s){ f - rep(NA, length(x)) f[1] - digamma(x[1]) - digamma(x[1]+x[2]) - s[1] f[2] - digamma(x[2]) - digamma(x[1]+x[2]) - s[2] f } require(BB) # load this package for the nonlinear solver s - c(-2, -4) # one row of s1 and s2 ans - dfsane(par=c(1,1), fn=fn, s=s) ans$par # solutions for p and q You can then loop through for each row of s1 and s2 and solve it to get corresponding p and q. Ravi. Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu - Original Message - From: Shaoqiong Zhao zh...@uwm.edu Date: Wednesday, March 10, 2010 8:19 pm Subject: [R] help about solving two equations To: r-help@r-project.org I have two matrix s1 and s2, each of them is 1000*1. and I have two equations: digamma(p)-digamma(p+q)=s1, digamma(q)-digamma(p+q)=s2, and I want to sovle these two equations to get the value of x and y, which are also two 1000*1 matrices. I write a program like this: f - function(x) { p- x[1]; q - x[2]; ((digamma(p)-digamma(p+q)-s1[2,]) )^2 +((digamma(q)-digamma(p+q)-s2[2,]) )^2 } s - 1:10/10 g - expand.grid(p = s, q = s) idx - which.min(apply(g, 1, f)) idx g[idx,] I am not sure if this is correct and also this can only solve one row. How to get the whole 1000 rows of p and q? Thanks. Annie __ R-help@r-project.org mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. __ 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] help about solving two equations
Shaoqiong Zhao wrote: I am not sure if this is correct and also this can only solve one row. How to get the whole 1000 rows of p and q? You can try something like this: library(nleqslv) library(BB) fn - function(x, s){ f - rep(NA, length(x)) f[1] - digamma(x[1]) - digamma(x[1]+x[2]) - s[1] f[2] - digamma(x[2]) - digamma(x[1]+x[2]) - s[2] f } xstart - c(1,1) Krep - 100 s1 - rnorm(Krep,mean=-2,sd=.5) s2 - rnorm(Krep,mean=-4,sd=.5) sz - cbind(s1,s2) xans - matrix(ncol=2,nrow=Krep) system.time( { noncvg - 0 for(k in seq(Krep)) { ans - nleqslv(xstart,fn, s=sz[k,],method=Newton) xans[k,] - ans$x if(ans$termcd1){noncvg-noncvg+1} } msg - sprintf(Non convergence %d == %.2f%%\n, noncvg, noncvg/Krep*100) print(msg) }) system.time( { noncvg - 0 for(k in seq(Krep)) { ans - dfsane(par=xstart, fn=fn, s=sz[k,],control=list(trace=FALSE)) xans[k,] - ans$par if(ans$convergence0){noncvg-noncvg+1} } msg - sprintf(Non convergence %d == %.2f%%\n, noncvg, noncvg/Krep*100) print(msg) }) It seems that nleqslv finds more solutions than dfsane and is also quite a bit quicker. Berend -- View this message in context: http://n4.nabble.com/help-about-solving-two-equations-tp1588313p1588731.html Sent from the R help mailing list archive at Nabble.com. __ 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] help about solving two equations
You have to install it from CRAN before you can load it into your session. Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu - Original Message - From: Shaoqiong Zhao zh...@uwm.edu Date: Thursday, March 11, 2010 9:00 pm Subject: Re: [R] help about solving two equations To: Ravi Varadhan rvarad...@jhmi.edu Cc: r-help@r-project.org Hello Professor Ravi, I tried to load BB into R, but I got the following message: library(BB) Error in library(BB) : there is no package called 'BB' library(BB) Error in library(BB) : there is no package called 'BB' Can you tell me why? Thanks a lot. Annie - Original Message - From: Ravi Varadhan rvarad...@jhmi.edu To: Shaoqiong Zhao zh...@uwm.edu Cc: r-help@r-project.org Sent: Wednesday, March 10, 2010 9:03:25 PM GMT -06:00 US/Canada Central Subject: Re: [R] help about solving two equations Here is how you can solve: fn - function(x, s){ f - rep(NA, length(x)) f[1] - digamma(x[1]) - digamma(x[1]+x[2]) - s[1] f[2] - digamma(x[2]) - digamma(x[1]+x[2]) - s[2] f } require(BB) # load this package for the nonlinear solver s - c(-2, -4) # one row of s1 and s2 ans - dfsane(par=c(1,1), fn=fn, s=s) ans$par # solutions for p and q You can then loop through for each row of s1 and s2 and solve it to get corresponding p and q. Ravi. Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu - Original Message - From: Shaoqiong Zhao zh...@uwm.edu Date: Wednesday, March 10, 2010 8:19 pm Subject: [R] help about solving two equations To: r-help@r-project.org I have two matrix s1 and s2, each of them is 1000*1. and I have two equations: digamma(p)-digamma(p+q)=s1, digamma(q)-digamma(p+q)=s2, and I want to sovle these two equations to get the value of x and y, which are also two 1000*1 matrices. I write a program like this: f - function(x) { p- x[1]; q - x[2]; ((digamma(p)-digamma(p+q)-s1[2,]) )^2 +((digamma(q)-digamma(p+q)-s2[2,]) )^2 } s - 1:10/10 g - expand.grid(p = s, q = s) idx - which.min(apply(g, 1, f)) idx g[idx,] I am not sure if this is correct and also this can only solve one row. How to get the whole 1000 rows of p and q? Thanks. Annie __ R-help@r-project.org mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. __ 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] help about solving two equations
Hello Professor Ravi, I tried to load BB into R, but I got the following message: library(BB) Error in library(BB) : there is no package called 'BB' library(BB) Error in library(BB) : there is no package called 'BB' Can you tell me why? Thanks a lot. Annie - Original Message - From: Ravi Varadhan rvarad...@jhmi.edu To: Shaoqiong Zhao zh...@uwm.edu Cc: r-help@r-project.org Sent: Wednesday, March 10, 2010 9:03:25 PM GMT -06:00 US/Canada Central Subject: Re: [R] help about solving two equations Here is how you can solve: fn - function(x, s){ f - rep(NA, length(x)) f[1] - digamma(x[1]) - digamma(x[1]+x[2]) - s[1] f[2] - digamma(x[2]) - digamma(x[1]+x[2]) - s[2] f } require(BB) # load this package for the nonlinear solver s - c(-2, -4) # one row of s1 and s2 ans - dfsane(par=c(1,1), fn=fn, s=s) ans$par # solutions for p and q You can then loop through for each row of s1 and s2 and solve it to get corresponding p and q. Ravi. Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu - Original Message - From: Shaoqiong Zhao zh...@uwm.edu Date: Wednesday, March 10, 2010 8:19 pm Subject: [R] help about solving two equations To: r-help@r-project.org I have two matrix s1 and s2, each of them is 1000*1. and I have two equations: digamma(p)-digamma(p+q)=s1, digamma(q)-digamma(p+q)=s2, and I want to sovle these two equations to get the value of x and y, which are also two 1000*1 matrices. I write a program like this: f - function(x) { p- x[1]; q - x[2]; ((digamma(p)-digamma(p+q)-s1[2,]) )^2 +((digamma(q)-digamma(p+q)-s2[2,]) )^2 } s - 1:10/10 g - expand.grid(p = s, q = s) idx - which.min(apply(g, 1, f)) idx g[idx,] I am not sure if this is correct and also this can only solve one row. How to get the whole 1000 rows of p and q? Thanks. Annie __ R-help@r-project.org mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. __ 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] help about solving two equations
Here is how you can solve: fn - function(x, s){ f - rep(NA, length(x)) f[1] - digamma(x[1]) - digamma(x[1]+x[2]) - s[1] f[2] - digamma(x[2]) - digamma(x[1]+x[2]) - s[2] f } require(BB) # load this package for the nonlinear solver s - c(-2, -4) # one row of s1 and s2 ans - dfsane(par=c(1,1), fn=fn, s=s) ans$par # solutions for p and q You can then loop through for each row of s1 and s2 and solve it to get corresponding p and q. Ravi. Ravi Varadhan, Ph.D. Assistant Professor, Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins University Ph. (410) 502-2619 email: rvarad...@jhmi.edu - Original Message - From: Shaoqiong Zhao zh...@uwm.edu Date: Wednesday, March 10, 2010 8:19 pm Subject: [R] help about solving two equations To: r-help@r-project.org I have two matrix s1 and s2, each of them is 1000*1. and I have two equations: digamma(p)-digamma(p+q)=s1, digamma(q)-digamma(p+q)=s2, and I want to sovle these two equations to get the value of x and y, which are also two 1000*1 matrices. I write a program like this: f - function(x) { p- x[1]; q - x[2]; ((digamma(p)-digamma(p+q)-s1[2,]) )^2 +((digamma(q)-digamma(p+q)-s2[2,]) )^2 } s - 1:10/10 g - expand.grid(p = s, q = s) idx - which.min(apply(g, 1, f)) idx g[idx,] I am not sure if this is correct and also this can only solve one row. How to get the whole 1000 rows of p and q? Thanks. Annie __ R-help@r-project.org mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. __ 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] help about solving two equations
Shaoqiong Zhao wrote: I have two matrix s1 and s2, each of them is 1000*1. and I have two equations: digamma(p)-digamma(p+q)=s1, digamma(q)-digamma(p+q)=s2, and I want to sovle these two equations to get the value of x and y, which are also two 1000*1 matrices. Besides BB there is another package for solving systems of equations: nleqslv. Use Ravi's definition of your function. fn - function(x, s){ f - rep(NA, length(x)) f[1] - digamma(x[1]) - digamma(x[1]+x[2]) - s[1] f[2] - digamma(x[2]) - digamma(x[1]+x[2]) - s[2] f } s - c(-2,-4) xstart - c(1,1) library(nleqslv) z1 - nleqslv(xstart,fn,s=s) z1 or for more detail z2 - nleqslv(xstart,fn,s=s, control=list(trace=1)) z2 Berend -- View this message in context: http://n4.nabble.com/help-about-solving-two-equations-tp1588313p1588503.html Sent from the R help mailing list archive at Nabble.com. __ 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] help about solving the equations
dahuang wrote: i wanna get x from the equations: Ax=x, given A is a matrix. How can i apply it in R? thanks ?solve -- View this message in context: http://www.nabble.com/help-about-solving-the-equations-tp25738073p25738401.html Sent from the R help mailing list archive at Nabble.com. __ 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] help about solving the equations
x is the eigenvector corresponding to eigenvalue 1. See ?eigen. On Sun, Oct 4, 2009 at 9:31 AM, dahuang tsyell...@hotmail.com wrote: i wanna get x from the equations: Ax=x, given A is a matrix. How can i apply it in R? thanks -- View this message in context: http://www.nabble.com/help-about-solving-the-equations-tp25738073p25738073.html Sent from the R help mailing list archive at Nabble.com. __ 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-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] help about solving the equations
Gabor Grothendieck wrote: x is the eigenvector corresponding to eigenvalue 1. See ?eigen. Correct. I should have looked better. Berend -- View this message in context: http://www.nabble.com/help-about-solving-the-equations-tp25738073p25740459.html Sent from the R help mailing list archive at Nabble.com. __ 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.