Error in cv.glmnet - r

I am using the following code getting the error message in R version 2.13.1. in mac.
cv.glmnet(dsgn.mat,resp,family="gaussian",nfolds=5)
Error in as.matrix(cbind2(1, newx) %*% nbeta) :
error in evaluating the argument 'x' in selecting a method for function 'as.matrix': Error in t(.Call(Csparse_dense_crossprod, y, t(x))) :
error in evaluating the argument 'x' in selecting a method for function 't': Error: invalid class 'NA' to dup_mMatrix_as_dgeMatrix
I should mention that there is no message if I use only glmnet then it is working fine.

Without any example data, all I can say is that, generally, most errors that pop up only during cross validation are because one of the random folds ends up "bad". This is obviously more common if you don't have many observations in your data set. For example, do you have any NAs in your data (might lead to an entire fold of NAs)?
You can get around this by preprocessing your data, but glmnet also will let you directly prescribe the folds via parameter foldid.

Related

EnvStats::epdfPlot() throws error with continuous distribution

I am trying to compute an empirical probability distribution for a continuous variable using the epdfPlot() function in the EnvStats:: package. I keep getting an error when I accept the default of discrete=FALSE.
Error in UseMethod("density") :
no applicable method for 'density' applied to an object of class "c('double', 'numeric')"
Reading through the documentation, I think this is somehow a result of how the function passes arguments to stats::density() because I don't have this problem when I set discrete = TRUE. As the documentation notes, the argument density.arg.list=NULL is ignored when discrete = TRUE. Here is the reproducible example:
library(EnvStats)
dat<-rnorm(500, 0, 1)
demo1<-epdfPlot(dat, discrete = FALSE, plot.it=FALSE) # throws error
demo2<-epdfPlot(dat, discrete = TRUE, plot.it=FALSE) # works
demo2
Is this possibly a bug?
Turns out this occurs because of a conflict with the labdsv::density() function. EnvStats::epdfPlot() doesn't specify stats::density(), so when density() gets masked by another package, EnvStats::epdfPlot() calls that function. I tried running the code in a different session and it worked without error.

How to run vegan::varpart without having this "NA/NaN/Inf in foreign function call (arg 1)" message error?

I am currently trying to run the varpart function on some of my data but I get the following error every time:
"Error in qr.default(X, tol = 1e-06) :
NA/NaN/Inf in foreign function call (arg 1)"
Shortly put, my whole dataset contains informations about fishing gears, levels of pressure and different sessions in time. I have no issue running the varpart function on all of my data but if I create a new object with only the data regarding a specific gear and/or a specific pressure it doesnt work anymore....
I checked for missing values but couldnt find one...
Here is an example of my problem:
library(vegan)
#> Le chargement a nécessité le package : permute
#> Le chargement a nécessité le package : lattice
#> This is vegan 2.5-5
A<-runif(45,min=0,max=0.75)
B<-runif(45,min=0,max=0.75)
C<-runif(45,min=0,max=0.75)
SP<-data.frame(cbind(A,B,C))
Session<-as.factor(rep(c("S1","S2","S3","S4","S5"),each=9))
LONG<-rep(c(48.32287917),each=45)
data<-data.frame(cbind(Session,LONG))
data$Session<-as.factor(data$Session)
varpart<-varpart(SP,~ Session,~ LONG, data=data)
#> Error in qr.default(X, tol = 1e-06) :
NA/NaN/Inf in foreign function call (arg 1)
Has any of you already encountered this kind of problem before or knows how to fix it ?
Thank you!
varpart performs variation partition, and for that it needs at least two variables for which to partition the variation. You had only one (Session) – the second one you supplied (LONG) was constant, and hence will explain no variation and was not counted as a variable. We do not check against non-varying "variables", and you get a confusing error message. However, if you are hand-crafting your model with meaningless (constant) "variables", you should expect weird error messages. We could add a test against constants, but somehow it feels strange.

Issue with LCMM Plot Code (invalid subscript type 'list')

I'm attempting to follow a tutorial (link here: https://www.r-bloggers.com/latent-class-mixed-models-with-graphics/) for running a latent class mixture model. My model has run properly, but i'm having an issue plotting the latent classes.
Con2 < lcmm(ConT~AdminCount,random=~AdminCount,subject='PID',mixture=~AdminCount,ng=3,idiag=TRUE,data=datal,link="linear")
summary(Con2)
datal$CONid <- as.character(datal$PID)
people3 <- as.data.frame(Con2$pprob[1:2])
datal$CONgroup <- character(people3$class[sapply(datal$CONid, function(x) which(people3$CONid==x))])
When I try to run the last line of code, I get this error:
Error in people3$class[sapply(datal$CONid, function(x)
which(people3$CONid == : invalid subscript type 'list'
Any ideas what this error means/how I can address it?
Pictures here of all my code and output:
info about variables being used in model
Probability values from model
Model Summary
Graph Code and Error
We still can't recreate your error; I did find the original tutorial posting though as well as the full R code and the data.
So I need to guess your error. Note that the error statement is that the "subscript is a list". The subscript is the function call sapply(datal$CONid, function(x) which(people3$CONid==x). In the default setting, the sapply-function will return a list, if the return values of the function are of irregular length (otherwise a vector or a matrix). That is, the elements of datal$CONid occur with irregular frequency among the entries of people3$CONid.
Hope you can work from here on.

Error: $ operator is invalid for atomic vectors

I am trying to make boxcox transformation of a variable (i.e. sqrt.CR) with lambda value from -2 to 2. On running the below R code it gives a error of invalid atomic vectors. Later on checking earlier posts i saw few suggestions to transform the matrix into a data frame. Though the error continued to show up. Do anyone know to figure out this error ?
R code.
Matrix to data frame conversion
drivers.data<-as.data.frame(drivers)
Boxcox transfrom.
drivers$box_CR<-boxcox(drivers.data$sqrt.CR,lambda=seq(-2,2))
The input to boxcox must be the output of a lm or aov call, not a vector of numbers as yours appears to be. See ?boxcox.
boxcox(object, ...)
Arguments:
object: a formula or fitted model object. Currently only ‘lm’ and
‘aov’ objects are handled.
It could be because of package conflict, in MASS,boxcox requires a model object lm, whereas in bestNormalize it requires a vector.
Try
bestNormalize::boxcox(drivers.data)

irr: Krippendorf's Alpha with non-numeric classifications (Warning Message)

I am trying to calculate Krippendorf's Alpha using the irr::kripp.alpha function.
My input data consists of non-numeric classifications (e.g., "1.a", "1.b" etc.). When using kripp.alpha() I get the following warning message
Warning message:
In kripp.alpha(as.matrix(p8)) : NAs introduced by coercion
It seems that the function -- nevertheless -- works fine.
Anyhow, I tried to get rid of the warning message by using the following procedure:
input <- data.frame(coder1=c("3.a","3.a","3.b.ii","3.b.ii","3.a","3.a","4.d","4.d"),
coder2=c("3.b","3.a","3.b.i","3.b.ii","3.a","3.a","4.d","4.d"))
# Gives the Warning Message
kripp.alpha(as.matrix(t(input)))
input <- unlist(input)
#Replace levels (strings) with unique numeric values
levels(input) <- 1:length(levels(input))
#Transform back into matrix format that kripp.alpha uses
input <- matrix(input, nrow=2)
kripp.alpha(input)
This works fine. However, it is quite cumbersome for such an easy task. Is there a simpler method? Can somebody explain why the function gives the warning message when using non-numeric classification values?
I just had contact with the package maintainer. The kripp.alpha() function expects numeric classifications as input. They may fix it in one of the next package updates.
So either you live with that warning message or you use the solution posted above.

Resources