[R] Need Help To Solve An Equation In R

2017-05-28 Thread Kenneth Knoblauch
This looks an awful lot like you are trying to solve for d' in an 
m-alternative forced choice experiment for an unbiased observer.  Try 
the function dprime.mAFC from the psyphy package.  For comparison with 
your example, it's code is:


 psyphy:::dprime.mAFC
function (Pc, m)
{
m <- as.integer(m)
if (m < 2)
stop("m must be an integer greater than 1")
if (!is.integer(m))
stop("m must be an integer")
if (Pc <= 0 || Pc >= 1)
stop("Pc must be in (0,1)")
est.dp <- function(dp) {
pr <- function(x) dnorm(x - dp) * pnorm(x)^(m - 1)
Pc - integrate(pr, lower = -Inf, upper = Inf)$value
}
dp.res <- uniroot(est.dp, interval = c(-10, 10))
dp.res$root
}


Hope that helps,

best,

Ken


On Sat, May 27, 2017 at 9:16 AM, Neetu Shah  
wrote:

Dear Sir/Ma'am,

I am trying to make a function to solve an equation that is given 
below:

findH <- function(p_star, k){
fun <- function(y){
(pnorm(y+h))^(k-1)*dnorm(y)
}
lhs <- integrate(fun, -Inf, Inf)$value
return(uniroot(lhs, lower = -10, upper = 10,
tol = p_star)$root)
}
In lhs, I integrated a function with respect to y and there would be an
unknown h that I need to find.
I need to equate this lhs to p_star value in order to find h. Which
function should I use to solve this equation?
Please guide me regarding this.
Thank you.

--
With Regards,
Neetu Shah,
B.Tech.(CS),
3rd Year,
IIIT Vadodara.

--
Kenneth Knoblauch
Inserm U1208
Stem-cell and Brain Research Institute
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.sbri.fr/members/kenneth-knoblauch.html

__
R-help@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.

Re: [R] Need Help To Solve An Equation In R

2017-05-27 Thread Bert Gunter
Then:

1. Always cc the list;

2. Read and follow the posting guide: no HTML, plain text only.


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, May 27, 2017 at 11:05 AM, Neetu Shah  wrote:
> Dear Sir,
>
> It is not homework. I am trying to develop a library in R as my project. My
> mentor said to ask this doubt on R forum. As I am not able to solve that
> equation in R. I need to know the alternative for uniroot function. Please
> help me regarding this.
>
>
> --
> With Regards,
> Neetu Shah,
> B.Tech.(CS),
> 3rd Year,
> IIIT Vadodara.
>
>
> On Sat, May 27, 2017 at 11:11 PM, Bert Gunter 
> wrote:
>>
>> Homework? We generally don't do homework here.
>>
>> Cheers,
>> Bert
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>>
>> On Sat, May 27, 2017 at 9:16 AM, Neetu Shah  wrote:
>> > Dear Sir/Ma'am,
>> >
>> > I am trying to make a function to solve an equation that is given below:
>> > findH <- function(p_star, k){
>> > fun <- function(y){
>> > (pnorm(y+h))^(k-1)*dnorm(y)
>> > }
>> > lhs <- integrate(fun, -Inf, Inf)$value
>> > return(uniroot(lhs, lower = -10, upper = 10,
>> > tol = p_star)$root)
>> > }
>> > In lhs, I integrated a function with respect to y and there would be an
>> > unknown h that I need to find.
>> > I need to equate this lhs to p_star value in order to find h. Which
>> > function should I use to solve this equation?
>> > Please guide me regarding this.
>> > Thank you.
>> >
>> > --
>> > With Regards,
>> > Neetu Shah,
>> > B.Tech.(CS),
>> > 3rd Year,
>> > IIIT Vadodara.
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > __
>> > R-help@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@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.


Re: [R] Need Help To Solve An Equation In R

2017-05-27 Thread Bert Gunter
Homework? We generally don't do homework here.

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, May 27, 2017 at 9:16 AM, Neetu Shah  wrote:
> Dear Sir/Ma'am,
>
> I am trying to make a function to solve an equation that is given below:
> findH <- function(p_star, k){
> fun <- function(y){
> (pnorm(y+h))^(k-1)*dnorm(y)
> }
> lhs <- integrate(fun, -Inf, Inf)$value
> return(uniroot(lhs, lower = -10, upper = 10,
> tol = p_star)$root)
> }
> In lhs, I integrated a function with respect to y and there would be an
> unknown h that I need to find.
> I need to equate this lhs to p_star value in order to find h. Which
> function should I use to solve this equation?
> Please guide me regarding this.
> Thank you.
>
> --
> With Regards,
> Neetu Shah,
> B.Tech.(CS),
> 3rd Year,
> IIIT Vadodara.
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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@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.


Re: [R] Need Help To Solve An Equation In R

2017-05-27 Thread David Winsemius

> On May 27, 2017, at 9:16 AM, Neetu Shah  wrote:
> 
> Dear Sir/Ma'am,
> 
> I am trying to make a function to solve an equation that is given below:
> findH <- function(p_star, k){
> fun <- function(y){
> (pnorm(y+h))^(k-1)*dnorm(y)
> }
> lhs <- integrate(fun, -Inf, Inf)$value
> return(uniroot(lhs, lower = -10, upper = 10,
> tol = p_star)$root)
> }
> In lhs, I integrated a function with respect to y and there would be an
> unknown h that I need to find.

uniroot needs a function as its first argument. `integrate(fun, -Inf, 
Inf)$value` does not return a function. it will return a numeric value (if it 
succeeds). 

I have no idea whether this makes any mathematical sense because you have not 
described the background for this effort and have not shown how you expect to 
call `findH`. Is `h` supposed to be some sort of non-centrality parameter with 
`k` representing degrees of freedom?

I tried this code that does run, although it fails gracefully at runtime:

findH <- function(p_star= 0.004, k=2){
   fun <- function(y,h){
(pnorm(y+h))^(k-1)*dnorm(y)
   }
lhs <- function(x) { integrate(fun, lower=-Inf, upper=Inf, h=x)$value }
return(uniroot(lhs, lower = -10, upper = 10, tol = p_star)$root)
}
#---
> findH(k=6)
Error in uniroot(lhs, lower = -10, upper = 10, tol = p_star) : 
  f() values at end points not of opposite sign



> I need to equate this lhs to p_star value in order to find h.

> Which
> function should I use to solve this equation?
> Please guide me regarding this.
> Thank you.
> 
> -- 
> With Regards,
> Neetu Shah,
> B.Tech.(CS),
> 3rd Year,
> IIIT Vadodara.
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@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.

David Winsemius
Alameda, CA, USA

__
R-help@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] Need Help To Solve An Equation In R

2017-05-27 Thread Neetu Shah
Dear Sir/Ma'am,

I am trying to make a function to solve an equation that is given below:
findH <- function(p_star, k){
fun <- function(y){
(pnorm(y+h))^(k-1)*dnorm(y)
}
lhs <- integrate(fun, -Inf, Inf)$value
return(uniroot(lhs, lower = -10, upper = 10,
tol = p_star)$root)
}
In lhs, I integrated a function with respect to y and there would be an
unknown h that I need to find.
I need to equate this lhs to p_star value in order to find h. Which
function should I use to solve this equation?
Please guide me regarding this.
Thank you.

-- 
With Regards,
Neetu Shah,
B.Tech.(CS),
3rd Year,
IIIT Vadodara.

[[alternative HTML version deleted]]

__
R-help@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.