An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20080212/d6620f42/attachment.pl
How to make t.test handle "NA" and "essentially constant values" ?
9 messages · Ng Stanley, PIKAL Petr, ONKELINX, Thierry +3 more
Hi r-help-bounces at r-project.org napsal dne 12.02.2008 09:09:23:
Hi, First problem:
test <- matrix(c(1,1,2,1), 2,2)
apply(test, 1, function(x) { t.test(x) $p.value })
Error in t.test.default(x) : data are essentially constant
make your data not constant
Second problem:
test <- matrix(c(1,0,NA,1), 2,2)
apply(test, 1, function(x) { t.test(x) $p.value })
Error in t.test.default(x) : not enough 'x' observations
increase number of observations
How to make t-test ignores this errors ?
Well, the procedure is complaining that you do not give it correct data. You shall be gratefull for a great software which prevent you from making silly things as try to compute t.test when data have zero variantion or number of observations is 1. Regards Petr
[[alternative HTML version deleted]]
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20080212/c5bcfe17/attachment.pl
Have a look at ?try try(t.test(x)) HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Ng Stanley Verzonden: dinsdag 12 februari 2008 11:56 Aan: r-help Onderwerp: Re: [R] How to make t.test handle "NA" and "essentially constantvalues" ? Thanks. Someone please help to make t.test go through all the data and not to be disrupted by the two problems.
On 2/12/08, Petr PIKAL <petr.pikal op precheza.cz> wrote:
Hi r-help-bounces op r-project.org napsal dne 12.02.2008 09:09:23:
Hi, First problem:
test <- matrix(c(1,1,2,1), 2,2)
apply(test, 1, function(x) { t.test(x) $p.value })
Error in t.test.default(x) : data are essentially constant
make your data not constant
Second problem:
test <- matrix(c(1,0,NA,1), 2,2)
apply(test, 1, function(x) { t.test(x) $p.value })
Error in t.test.default(x) : not enough 'x' observations
increase number of observations
How to make t-test ignores this errors ?
Well, the procedure is complaining that you do not give it correct
data.
You shall be gratefull for a great software which prevent you from
making
silly things as try to compute t.test when data have zero variantion
or
number of observations is 1. Regards Petr
[[alternative HTML version deleted]]
______________________________________________ R-help op 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 op 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.
Do you mean to deal with the situation where you're doing many t-tests in a loop? If so there was a post very recently on this list about this: https://stat.ethz.ch/pipermail/r-help/2008-February/153254.html Richard.
Ng Stanley wrote:
Thanks. Someone please help to make t.test go through all the data and not to be disrupted by the two problems. On 2/12/08, Petr PIKAL <petr.pikal at precheza.cz> wrote:
Hi
r-help-bounces at r-project.org napsal dne 12.02.2008 09:09:23:
Hi,
First problem:
test <- matrix(c(1,1,2,1), 2,2)
apply(test, 1, function(x) { t.test(x) $p.value })
Error in t.test.default(x) : data are essentially constant
make your data not constant
Second problem:
test <- matrix(c(1,0,NA,1), 2,2)
apply(test, 1, function(x) { t.test(x) $p.value })
Error in t.test.default(x) : not enough 'x' observations
increase number of observations
How to make t-test ignores this errors ?
Well, the procedure is complaining that you do not give it correct data.
You shall be gratefull for a great software which prevent you from making
silly things as try to compute t.test when data have zero variantion or
number of observations is 1.
Regards
Petr
[[alternative HTML version deleted]]
______________________________________________ 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.
[[alternative HTML version deleted]]
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20080212/23012e70/attachment.pl
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20080212/7bfee33d/attachment.pl
Petr PIKAL wrote:
Hi r-help-bounces at r-project.org napsal dne 12.02.2008 09:09:23:
Hi, First problem:
test <- matrix(c(1,1,2,1), 2,2)
apply(test, 1, function(x) { t.test(x) $p.value })
Error in t.test.default(x) : data are essentially constant
make your data not constant
Second problem:
test <- matrix(c(1,0,NA,1), 2,2)
apply(test, 1, function(x) { t.test(x) $p.value })
Error in t.test.default(x) : not enough 'x' observations
increase number of observations
How to make t-test ignores this errors ?
Well, the procedure is complaining that you do not give it correct data. You shall be gratefull for a great software which prevent you from making silly things as try to compute t.test when data have zero variantion or number of observations is 1.
It's nice that the software recognizes situations in which a sensible
answer can't be computed. At that point, there are two possible actions:
(1) stop with an informative error, and (2) silently return NA. Option (1)
is wonderful for interactive use, but option (2) is easier to handle in
programs where one is making many calls to the function as part of some
automated procedure (e.g., as part of a bootstrap procedure).
Speaking from personal experience, it can be quite a drag when one has set
up and mostly-debugged a long computation only to have it stop with an
error like "data are essentially constant" right near the end because of
some condition for which the function author thought it better to stop with
an error rather than return NA (or some other indication that there was no
sensible answer) (didn't happen with t.test, but I've experienced it with a
few other functions.)
So, I don't think it's at all unreasonable for the OP to request a way to
make t.test() return NA instead of stopping with an error.
Looking at the code for t.test, it doesn't look like there's any argument
to specify such behavior, so the options are to write one's own version of
t.test, or use try() as other posters have suggested. Here's an example
using try():
> my.t.test.p.value <- function(...) {
+ obj<-try(t.test(...), silent=TRUE)
+ if (is(obj, "try-error")) return(NA) else return(obj$p.value)
+ }
> my.t.test.p.value(numeric(0))
[1] NA
> my.t.test.p.value(1:10)
[1] 0.000278196
> my.t.test.p.value(1)
[1] NA
> my.t.test.p.value(c(1,1,1))
[1] NA
> my.t.test.p.value(c(1,2,NA))
[1] 0.2048328
> my.t.test.p.value(c(1,2))
[1] 0.2048328
>
hope this helps,
Tony Plate
Regards Petr
[[alternative HTML version deleted]]
______________________________________________ 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. ______________________________________________ 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.
Speaking from personal experience, it can be quite a drag when one has set up and mostly-debugged a long computation only to have it stop with an error like "data are essentially constant" right near the end because of some condition for which the function author thought it better to stop with an error rather than return NA (or some other indication that there was no sensible answer) (didn't happen with t.test, but I've experienced it with a few other functions.) So, I don't think it's at all unreasonable for the OP to request a way to make t.test() return NA instead of stopping with an error. Looking at the code for t.test, it doesn't look like there's any argument to specify such behavior, so the options are to write one's own version of t.test, or use try() as other posters have suggested. Here's an example using try():
> my.t.test.p.value <- function(...) {
+ obj<-try(t.test(...), silent=TRUE) + if (is(obj, "try-error")) return(NA) else return(obj$p.value) + }
I've written the following functions to make these tasks a little easier:
try_default <- function (expr, default = NA) {
result <- default
tryCatch(result <- expr, error = function(e) {})
result
}
failwith <- function(default = NULL, f, ...) {
function(...) try_default(f(...), default)
}
so my.t.test.p.value could be created as:
my.t.test.p.value <- function(...) failwith(NA, t.test(...))
Hadley