Re: [R] How to compute a P-value for a complex mixture of chi-squared distributions in R

2014-05-22 Thread Duncan Murdoch
On 01/06/2013, 12:26 AM, Tiago V. Pereira wrote: Hello, R users! I am struggling with the following problem: I need to compute a P-value for a mixture of two chi-squared distributions. My P-value is given by: P = 0.5*prob(sqrt(chi2(1)) = x) + 0.5*prob(sqrt(chi2(2)) = x) Isn't this simply

Re: [R] How to compute a P-value for a complex mixture of chi-squared distributions in R

2014-05-22 Thread peter dalgaard
On 22 May 2014, at 14:44 , Duncan Murdoch murdoch.dun...@gmail.com wrote: On 01/06/2013, 12:26 AM, Tiago V. Pereira wrote: Hello, R users! I am struggling with the following problem: I need to compute a P-value for a mixture of two chi-squared distributions. My P-value is given by: P

Re: [R] How to compute a P-value for a complex mixture of chi-squared distributions in R

2013-06-03 Thread Terry Therneau
You need to be more explicit about what you are doing. For this problem: y = (x1 + x2)/2 where x1 and x2 are chi-square random variables, you want to use the pchisqsum() routine found in the survey package. This is not a trivial computation. For the alternate problem where y is a random

Re: [R] How to compute a P-value for a complex mixture of chi-squared distributions in R

2013-06-01 Thread Rui Barradas
Hello, Try the following. dmix - function(x){ dens - function(x, df) dchisq(x^2, df = df)*2*x 0.5*dens(x, df = 1) + 0.5*dens(x, df = 2) } pmix - function(x, lower.tail = TRUE){ p - integrate(dmix, lower = 0, upper = x) if(lower.tail) p$value else 1 - p$value }

Re: [R] How to compute a P-value for a complex mixture of chi-squared distributions in R

2013-06-01 Thread Rui Barradas
Hello, Or, if you want to pass a vector of quantiles to pmix, pmix - function(x, lower.tail = TRUE){ p - sapply(x, function(.x) integrate(dmix, lower = 0, upper = .x)$value) if(lower.tail) p else 1 - p } Rui Barradas Em 01-06-2013 11:13, Rui Barradas escreveu: Hello, Try

Re: [R] How to compute a P-value for a complex mixture of chi-squared distributions in R

2013-06-01 Thread peter dalgaard
On Jun 1, 2013, at 06:32 , Tiago V. Pereira wrote: Hello, R users! I am struggling with the following problem: I need to compute a P-value for a mixture of two chi-squared distributions. My P-value is given by: P = 0.5*prob(sqrt(chi2(1)) = x) + 0.5*prob(sqrt(chi2(2)) = x) In words,

Re: [R] How to compute a P-value for a complex mixture of chi-squared distributions in R

2013-06-01 Thread Rui Barradas
Hello, No, nothing wrong. (I feel silly for not having noticed it.) In fact not only it's much simpler but it's also more accurate than the use of accurate with the default rel.tol. It should be better, however, to use lower.tail = FALSE, since the op wants p-values. 0.5 * pchisq(x^2, 1,

Re: [R] How to compute a P-value for a complex mixture of chi-squared distributions in R

2013-06-01 Thread Tiago V. Pereira
Thank you very much, Rui and Peter, for you detailed and helpful tips! It worked like a charm! I would spend more two weeks (or more) to figure out that by myself. Cheers! Tiago Hello, No, nothing wrong. (I feel silly for not having noticed it.) In fact not only it's much simpler but it's

Re: [R] How to compute a P-value for a complex mixture of chi-squared distributions in R

2013-06-01 Thread Rui Barradas
Inline. Em 01-06-2013 19:41, Rui Barradas escreveu: Hello, No, nothing wrong. (I feel silly for not having noticed it.) In fact not only it's much simpler but it's also more accurate than the use of accurate with the default rel.tol. Correction: ...than the use of _integrate()_ with the