"data are essentially constant" error with t test - r

t.test(antibioticdata$Bacteria,
antibioticdata$Inhibition,
alternative = c("two.sided"),
paired = FALSE,
var.equal = FALSE)
Here is my R code to make a t-test for a set of data on antibiotic resistance of bacteria. This gives me the error code:
Error in if (stderr < 10 * .Machine$double.eps * max(abs(mx), abs(my))) stop("data are essentially constant") :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In mean.default(x) : argument is not numeric or logical: returning NA
2: In var(x) :
Calling var(x) on a factor x is deprecated and will become an error.
Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.
not sure what I am doing wrong

I just met the same error. It's probably due to all the values in each group are the same.
So just write two more "if else". For me, I did
library("greenbrown")
apply(data.table, 1, function(x){
if(AllEqual(x[1:9])){return(1)}
else if(AllEqual(x[1:4]) & AllEqual(x[5:9])){return(0)} else {
t.test(as.numeric(x[1:4]), as.numeric(x[5:9]))->t.results
return(t.results$p.value)
}
})->P.for.data.table

Related

R: error in tableplot: Error in if (by < 1) stop("'by' must be > 0")

I'm a beginner in R and cannot understand what's the problem in that simple code:
install.packages("tabplot")
library("tabplot")
library("MASS")
Boston$chas <- factor(Boston$chas)
Boston$rad <- ordered(Boston$rad)
tableplot(Boston)
After running the function 'tableplot' I get this error message:
Error in if (by < 1) stop("'by' must be > 0") :
missing value where TRUE/FALSE needed
`In addition: Warning message:
In chunk.default(from = 1L, to = 506L, by = c(double = 23058430092136940), :NAs introduced by coercion to integer range
What is the problem? There're no missing values in the dataset. Could someone explain it, please?
Many thanks in advance
Daria

PCA with result non-interactively in R

I send you a message because I would like realise an PCA in R with the package ade4.
I have the data "PAYSAGE" :
All the variables are numeric, PAYSAGE is a data frame, there are no NAS or blank.
But when I do :
require(ade4)
ACP<-dudi.pca(PAYSAGE)
2
I have the message error :
**You can reproduce this result non-interactively with:
dudi.pca(df = PAYSAGE, scannf = FALSE, nf = NA)
Error in if (nf <= 0) nf <- 2 : missing value where TRUE/FALSE needed
In addition: Warning message:
In as.dudi(df, col.w, row.w, scannf = scannf, nf = nf, call = match.call(), :
NAs introduced by coercion**
I don't understand what does that mean. Have you any idea??
Thank you so much
I'd suggest sharing a data set/example others could access, if possible. This seems data-specific and with NAs introduced by coercion you may want to check the type of your input - typeof(PAYSAGE) - the manual for dudi.pca states it takes a data frame of numeric values as input.
Yes, for example :
ag_div <- c(75362,68795,78384,79087,79120,73155,58558,58444,68795,76223,50696,0,17161,0,0)
canne <- c(rep(0,10),5214,6030,0,0,0)
prairie_el<- c(60, rep(0,13),76985)
sol_nu <- c(18820,25948,13150,9903,12097,21032,35032,35504,25948,20438,12153,33096,15748,33260,44786)
urb_peu_d <- c(448,459,5575,5902,5562,458,6271,6136,459,1850,40,13871,40,13920,28669)
urb_den <- c(rep(0,12),14579,0,0)
veg_arbo <- c(2366,3327,3110,3006,3049,2632,7546,7620,3327,37100,3710,0,181,0,181)
veg_arbu <- c(18704,18526,15768,15527,15675,18886,12971,12790,18526,15975,22216,24257,30962,24001,14523)
eau <- c(rep(0,10),34747,31621,36966,32165,28054)
PAYSAGE<-data.frame(ag_div,canne,prairie_el,sol_nu,urb_peu_d,urb_den,veg_arbo,veg_arbu,eau)
require(ade4)
ACP<-dudi.pca(PAYSAGE)

How to capture particular warning message and execute call

Lately when I run my code that uses coxph in the survival package
coxph(frml,data = data), I am now getting warning messages of the following type
1: In model.matrix.default(Terms, mf, contrasts = contrast.arg) :
partial argument match of 'contrasts' to 'contrasts.arg'
2: In seq.default(along = temp) :
partial argument match of 'along' to 'along.with'"
I'm not exactly sure why all of a sudden these partial argument match warnings started popping up, but I don't think they effect me.
However, when I get the following warning message, I want coxph(frml,data = data) = NA
3: In fitter(X, Y, strats, offset, init, control, weights = weights, :
Loglik converged before variable 2 ; beta may be infinite.
6: In coxph(frml, data = data) :
X matrix deemed to be singular; variable 1 3 4
I used tryCatch when I wasn't getting the partial argument match warning using this code where if the nested tryCatch got either a warning or error message it would return NA
coxphfit = tryCatch(tryCatch(coxph(frml,data = data), error=function(w) return(NA)), warning=function(w) return(NA))
However, now that I am getting the partial argument match warnings, I need to only return an NA if there is an error or if I get the above warning messages 3 and 4 . Any idea about how to capture these particular warning messages and return an NA in those instances?
It's actually interesting question, if you are looking for quick and dirty way of capturing warnings you could simply do:
withCallingHandlers({
warning("hello")
1 + 2
}, warning = function(w) {
w ->> w
}) -> res
In this example the object w created in parent environment would be:
>> w
<simpleWarning in withCallingHandlers({ warning("hello") 1 + 2}, warning = function(w) { w <<- w}): hello>
You could then interrogate it:
grepl(x = w$message, pattern = "hello")
# [1] TRUE
as
>> w$message
# [1] "hello"
Object res would contain your desired results:
>> res
[1] 3
It's not the super tidy way but I reckon you could always reference object w and check if the warning message has the phrase you are interested in.

"argument is of length zero" error in RandomForest tuneRF()

I used this code,
tuneRF(x = mtry[,c(4:18)],
y = mtry$Os, ntreeTry=50, stepFactor=2,
improve=0.05,trace=TRUE, plot=TRUE, doBest=FALSE)
And got the error:
Error in if (errorOld < 0) stop("Initial setting gave 0 error and no room for improvement.") :
argument is of length zero
What should I change?
I ran into this same error when my I accidentally typed a column that didn't exist for the response column. Perhaps your mtry$Os column does not exist (it's hard to solve your problem w/o a reproducible example).
Example:
> library(randomForest)
> data("iris")
> tuneRF(x = iris[1:4,], y = iris$notacol)
Error in if (errorOld < 0) stop("Initial setting gave 0 error and no room for improvement.") :
argument is of length zero

simple function gives result as NaNs?

I wrote a simple function but I wonder why I am getting NaNs as results.
Here is the code:
data
en1=c(1,9,5,9,8,7)
en=c(1,5,4,9,3,7)
en2=c(1,4,5,4,8,7)
function:
run:
err1(en,en1,en2)
result:
[1] NaN
Warning message:
In sqrt(mean(((pr - ob) * (ob - ref)), na.rm = na.rm)) : NaNs produced
Any idea on this please
You obtain NaN since the square root of a negative number is undefined (for real numbers).
You might wish to use abs in your function:
err1 <- function(pr,ob,ref,na.rm=TRUE){
sqrt(mean(abs((pr-ob)*(ob-ref)), na.rm = na.rm))}
err1(en,en1,en2)
[1] 1.825742

Resources