Error in wilcox.test : object 'x' not found - r

I'm trying to complete a mann-whitney-wilcoxon test in R to compare brood sizes between 2 years. My data read in successfully in 2 columns, labeled x and y for each year, ranked, with unequal sample sizes. I'm getting the following error and I'm not sure what the problem is.
setwd('c:/OSPR NEST 2011 & 2012')
penob1112<-read.csv('compare_penob_11_12.csv',header=TRUE)
wilcox.test(x, y, data=penob1112)
Error in wilcox.test(x, y, data = penob1112) : object 'x' not found
Thanks for any insights!

The data argument is only taken when the first argument is of class formula. You need to explicitly call each object instead:
wilcox.test(penob1112$x, penob1112$y)
Look at ?wilcox.test - it has two methods (default and formula)

Related

How to find the coefficient of variation using the tapply function in R

I am trying to find the coefficient of variation in abundance using tapply in R and tried this code:
tapply(ReefFish$count, ReefFish$commonname, FUN = 100*sd(x)/mean(x))
However, I get the error:
Error in is.data.frame(x) : object 'x' not found
I am not sure what to put as 'x' since I am looking for the coefficient of variation in abundance (count) by their common name (commonname).
Does anyone know how I am supposed to define x in this code?
I think you are looking for an anonymous function with a variable x.
tapply(ReefFish$count, ReefFish$commonname, function(x) 100*sd(x)/mean(x))

Error in sparse.model.matrix creation

I'm trying to create a sparse.model.matrix (from the Matrix package) with a formula where there is an interaction between two factors. This is fine if my input data has multiple rows but as soon as I have just one row I get the error:
Error in model.spmatrix(t, data, transpose = transpose, drop.unused.levels = drop.unused.levels, : cannot get a slot ("Dim") from an object of type "double"
For example: This doesn't work:
f<-(mpg~as.factor(cyl)*as.factor(hp))
y<-mtcars
y$cyl<-as.factor(y$cyl)
y$hp<-as.factor(y$hp)
x<-y[1,]
myMatrix<-Matrix::sparse.model.matrix(f,x)
However duplicating x across two rows causes the error to disappear:
x<-rbind(x,x)
myMatrix<-Matrix::sparse.model.matrix(f,x)
I've traced the error to
Matrix:::Csparse_vertcat / Matrix:::Csparse_horzcat within Matrix:::model.spmatrix but am unable to work out what this function, which is written in C, is trying to do. Any ideas? Please note as far as I can determine this error only occurs when processing the matrix creation for an interaction between two factors.

Correlating multiple columns in a matrix to a single column in the same matrix

I have a matrix of 48 variables with 40 observation each. I am trying to correlate the first 47 columns separately to the 48th column. what I've tried to do is use cor command:
cor(x[,1:47], x[,48], method="kendall").
I'm getting an error:
Error in cor.test.default(Hj1[, 1:47], Hj1[48], method = "kendall") :
'x' and 'y' must have the same length
Since each column is the same length, I understand it's not about the column lengths, but something else. what can it be?
Thanks!
David.
Look at your error message:
Error in cor.test.default(Hj1[, 1:47], Hj1[48], method = "kendall") :
'x' and 'y' must have the same length
Hj1[48].
That was a typo. You wanted Hj1[,48]
Ok, I've separated the column from the matrix and run the correlation successfully.
In my original code it looks like this:
Hj1tox <- Hj1[,48]
Hj1_ab <- Hj1[,1:47]
cor(Hj1_ab, Hj1tox)

What does "argument to 'which' is not logical" mean in FactoMineR MCA?

I'm trying to run an MCA on a datatable using FactoMineR. It contains only 0/1 numerical columns, and its size is 200.000 * 20.
require(FactoMineR)
result <- MCA(data[, colnames, with=F], ncp = 3)
I get the following error :
Error in which(unlist(lapply(listModa, is.numeric))) :
argument to 'which' is not logical
I didn't really know what to do with this error. Then I tried to turn every column to character, and everything worked. I thought it could be useful to someone else, and that maybe someone would be able to explain the error to me ;)
Cheers
Are the classes of your variables character or factor?I was having this problem. My solution was to change al variables to factor.
#my data.frame was "aux.da"
i=0
while(i < ncol(aux.da)){
i=i+1 aux.da[,i] = as.factor(aux.da[,i])
}
It's difficult to tell without further input, but what you can do is:
Find the function where the error occurred (via traceback()),
Set a breakpoint and debug it:
trace(tab.disjonctif, browser)
I did the following (offline) to find the name of tab.disjonctif:
Found the package on the CRAN mirror on GitHub
Search for that particular expression that gives the error
I just started to learn R yesterday, but the error comes from the fact that the MCA is for categorical data, so that's why your data cannot be numeric. Then to be more precise, before the MCA a "tableau disjonctif" (sorry i don't know the word in english : Complete disjunctive matrix) is created.
So FactomineR is using this function :
https://github.com/cran/FactoMineR/blob/master/R/tab.disjonctif.R
Where i think it's looking for categorical values that can be matched to a numerical value (like Y = 1, N = 0).
For others ; be careful : for R categorical data is related to factor type, so even if you have characters you could get this error.
To build off #marques, #Khaled, and #Pierre Gourseaud:
Yes, changing the format of your variables to factor should address the error message, but you shouldn't change the format of numerical data to factor if it's supposed to be continuous numerical data. Rather, if you have both continuous and categorical variables, try running a Factor Analysis for Mixed Data (FAMD) in the same FactoMineR package.
If you go the FAMD route, you can change the format of just your categorical variable columns to factor with this:
data[,c(3:5,10)] <- lapply(data[,c(3:5,10)] , factor)
(assuming column numbers 3,4,5 and 10 need to be changed).
This will not work for only numeric variables. If you only have numeric use PCA. Otherwise, add a factor variable to your data frame. It seems like for your case you need to change your variables to binary factors.
Same problem as well and changing to factor did not solve my answer either, because I had put every variable as supplementary.
What I did first was transform all my numeric data to factor :
Xfac = factor(X[,1], ordered = TRUE)
for (i in 2:29){
tfac = factor(X[,i], ordered = TRUE)
Xfac = data.frame(Xfac, tfac)
}
colnames(Xfac)=labels(X[1,])
Still, it would not work. But my 2nd problem was that I included EVERY factor as supplementary variable !
So these :
MCA(Xfac, quanti.sup = c(1:29), graph=TRUE)
MCA(Xfac, quali.sup = c(1:29), graph=TRUE)
Would generate the same error, but this one works :
MCA(Xfac, graph=TRUE)
Not transforming the data to factors also generated the problem.
I posted the same answer to a related topic : https://stackoverflow.com/a/40737335/7193352

"Error in 1:ncol(x) : argument of length 0" when using Amelia in R

I am working with panel data. I have well over 6,000 country-year observations, and have specified my Amelia imputation as follows:
(CountDependentVariable, m=5, ts="year", cs="cowcode",
sqrts=c("OtherCountVariable2", "OtherCount3", "OtherCount4"),
ords=c("OrdinalVar1", "Ordinal Variable 2"),
lgstc=c("ProportionVariale"),
noms=c("NominalVar1"),p2s = 0, idvars = c("country"))
When I run those lines of code, I continue to receive the following error:
Error in 1:ncol(x) : argument of length 0
I've seen people get a similar error, but in different contexts. Importantly, there are several continuous independent variables I left out of the Amelia code, because I am under the impression that they get imputed WITHOUT having to do so. Does anyone know:
1) What this error means?
2) How to correct this error?
Update #1: Provided more context, in terms of the types of variables in my count panel data, in the above sample code.
Update #2: I did some research, and ran into an R file containing a function that diagnoses possible errors for Amelia code. After running the code, I got the following error message first (and many more thereafter):
AMn<-nrow(x)
Error in nrow(x) : object 'x' not found
AMp<-ncol(x)
Error in ncol(x) : object 'x' not found
subbedout<-c(idvars,cs,ts)
Error: object 'idvars' not found
Error Code: 4
if (any(colSums(!is.na(x)) <= 1)) {
all.miss <- colnames(x)[colSums(!is.na(x)) <= 1]
if (is.null(all.miss)) {
all.miss <- which(colSums(!is.na(x)) <= 1)
}
all.miss <- paste(all.miss, collapse = ", ")
error.code<-4
error.mess<-paste("The data has a column that is completely missing or only has one,observation. Remove these columns:", all.miss)
return(list(code=error.code,mess=error.mess))
}
Error in is.data.frame(x) : object 'x' not found
Error codes: 5-6
Errors in one of the list variables
idout<-listcheck(idvars,"One of the 'idvars'")
Error in identical(vars, NULL) : object 'idvars' not found
Currently, there are no missing values for the country variable I place in the idvars argument. However, the very first "chunk" of errors wants me to believe that this is so.
Am I not properly specifying the Amelia code I have above?
I had forgotten to specify the dataframe in the original Amelia code (slaps hand on forehead). So now, after resolving the whacky issue above, I am getting the following error from Amelia:
Amelia Error Code: 44
One of the variable names in the options list does not match a variable name in the data.
I've checked the variable names, and they match, verbatim, to what I named them in the dataframe.

Resources