I've just tried to carry out an adonis2 test using the following code
adonis2(abundance.data~Site, data=df, method="bray") as in a previous post How can I fix an error with adonis2 test?
However I keep getting this error message
Error in if (any(lhs < -TOL)) stop("dissimilarities must be non-negative") :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In vegdist(as.matrix(lhs), method = method, ...) :
you have empty rows: their dissimilarities may be
meaningless in method “bray”
2: In vegdist(as.matrix(lhs), method = method, ...) :
missing values in results
Just wondering why this may be and how I can fix this issue
Simply speaking, I have a function f(x, t2) and I want to find the value of x that maximize the integral of f(x, t2) with respect to t2. I choose pso algorithm to do the optimization. The excutable code is as follows
library(pso)
xl=0; xu=2000; n=1; t2l=100; t2u=2000; t1=1
g<-function(x, t2) t1*x/(t2+x)
h<-function(z) 1/z^n
gdot<-function(x, t2){
c(x/(t2+x),-t1*x/(t2+x)^2)
}
logdetHinv<-function(dp, dw, t2){
gmat=mapply(function(x) gdot(x,t2),dp)
D0=gmat%*%diag(dw)%*%t(gmat)
D1=gmat%*%diag(1/h(g(dp,t2)))%*%diag(dw)%*%t(gmat)
2*log(det(D1))-log(det(D0))
}
obj<-function(x){
dp=x[1:2]; dw=c(x[3],1-x[3])
fitness_value=-integrate(Vectorize(function(t2) logdetHinv(dp, dw, t2)*1/(t2u-t2l)), t2l, t2u)$value
return(ifelse(dw[2]>0, fitness_value, fitness_value+1e3))
}
x <- psoptim(rep(1,3), fn = obj, lower = c(rep(xl,2),0.1), upper = c(rep(xu,2), 0.9))$par
x
Because the global optimization involves some random procedure, it sometimes reports the correct result
> x
[1] 2000.0000 754.4146 0.5000
the other times it reports error
Error in integrate(Vectorize(function(t2) logdetHinv(dp, dw, t2) * 1/(t2u - :
non-finite function value
In addition: There were 11 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: In log(det(D1)) : NaNs produced
2: In log(det(D0)) : NaNs produced
3: In log(det(D1)) : NaNs produced
4: In log(det(D0)) : NaNs produced
I suppose the algorithm tries take log of some negative values in logdetHinv, which returns NaN with a warning message, not an error yet, and finally causes error in integrate.
I want to avoid such values, maybe with tryCatch, like if there is warning in the function logdetHinv, it returns a very small value, but not NaN, so it will not cause error in integrate, and the psoptim is unlikely to choose such values when maximizing the objective function (minimizing -integrate(logdetHinv)) . I am not familiar with tryCatch in such complex situation. Where should I put the tryCatch? Thanks.
Moreover, I would like to know if there are some debugging techniques in R that allow me to know what random value (D0/D1) cause the error in this case. I guess it is some negative value in log, but it should not, as inside the log is a determinant of a positive definite matrix. In the traceback mode, in browse, if I type D0 the object 'D0' will not be found.
In this case, I would not use tryCatch which is usually more appropriate in testing than in your main code. Why don't you simply test the determinants in your function ? Something like that should work:
logdetHinv<-function(dp, dw, t2){
gmat=mapply(function(x) gdot(x,t2),dp)
D0=gmat%*%diag(dw)%*%t(gmat)
D1=gmat%*%diag(1/h(g(dp,t2)))%*%diag(dw)%*%t(gmat)
detD1 <- max(0.01, det(D1))
detD0 <- max(0.01, det(D0))
2*log(detD1)-log(detD0)
}
I used NbClust with the example data mtcars in R and found error.
How to fix this?
data(mtcars)
mtcars.scale=scale(mtcars)
library(NbClust)
nc = NbClust(mtcars.scale, min.nc = 2,max.nc = 5, method = "kmeans")
Error in solve.default(W) :
system is computationally singular: reciprocal condition number =
6.94466e-17
In addition: Warning message:
In log(det(P)/det(W)) : `NaNs produced`
You need to fix colinearity on your data (mtcars).
The error means your design matrix is not invertible and therefore can't be used to develop a regression model. This results from linearly dependent columns, i.e. strongly correlated variable.
https://stats.stackexchange.com/questions/76488/error-system-is-computationally-singular-when-running-a-glm
I do not have a reproducible example here but I am hoping to see if the following error is diagnostic of an obvious problem.
Using miceadds, I am running correlations on data that has been imputed with MICE. I receive the following error when a continuous variable ("6") (-0.5 to 1.4) is added to the model.
micombine.cor(imp, variables=c(3:4:5:6))
Above results in the following error
Error in stats::cor(dat_ii, dat_jj, method = method, use =
"pairwise.complete.obs") :
'y' must be numeric
In addition: Warning messages:
1: In 3:4:5 : numerical expression has 2 elements: only the first used
2: In 3:4:5:6 :
numerical expression has 3 elements: only the first used
I'm trying to run a factor analysis on a set of 80 dichotomous variables (1440 cases) using the hector function from the polycor package and the instructions I found here: http://researchsupport.unt.edu/class/Jon/Benchmarks/BinaryFA_L_JDS_Sep2014.pdf
Sadly, after I select just the variables interest from the rest of my dataset and run the factor analysis on them, I seem to consistently get the following error and warnings
Error in optim(0, f, control = control, hessian = TRUE, method = "BFGS") :
non-finite finite-difference value [1]
In addition: Warning messages:
1: In log(P) : NaNs produced
2: In log(P) : NaNs produced
This is with the command/when I hit the step described in the above PDF:
testMat <- hetcor(data)$cor
No idea what this means or how to proceed... Your thoughts are appreciated. Thank you!