EnvStats::epdfPlot() throws error with continuous distribution - r

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.

Related

"Error in svd(c) : infinite or missing values in 'x'" when using synth() in R

I am using the synth package in R to implement a synthetic control method, where I use the dataprep() function to construct the appropriate matrices to be passed to synth(). That is, I call dataprep() as follows:
dataprep_out <- dataprep(foo = csv_data,
predictors = vars_dep,
predictors.op = "mean",
time.predictors.prior = 2000:2010,
dependent = "Log_gdp",
unit.variable = "REG_FACTOR",
unit.names.variable = "REG_ID",
time.variable = "Year",
treatment.identifier = my_factor_treated,
controls.identifier = my_controls,
time.optimize.ssr = 2000:2010,
time.plot = 2000:2017
)
after which I call synth():
synth_out <- synth(data.prep.obj = dataprep_out)
This works fine and gives me the results I expect. However, when I repeat the same piece of code for another treated observation but with exactly the same controls (i.e., my_factor_treated is the only argument in dataprep() that has changed), I get the following error upon calling synth():
Error in svd(c) : infinite or missing values in 'x'.
I am struggling to find the cause of this error, also because I am unsure which object is being passed to the svd() function during the execution of synth(). None of the columns in the objects returned by dataprep() contain only zeroes, and they contain no Inf values (which makes sense, because otherwise this error should have occurred on the first treated observation as well, right?).
I would appreciate if someone could tell me why this error occurs and how I can prevent it. I have checked out multiple related questions but haven't been able to find my answer. Thanks!
PS. I am not sure how to provide a suitable MWE since I guess my problem is data-related and I won't be able to share the dataset that I am using.
I encountered the same issue, and the same as you did I confirmed there is no missing value or all 0's in my data set. Later I realized this is caused by optimization algorithm used in generating weights. One thing you can try is to add argument "optimxmethod='All'" in synth function. This will try all available methods and report you the one with best performance.

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.

Calculating variance covariance matrix for improved kppm model

I get the following error when I try to improve the intensity estimate of a kppm object, if I include the argument vcov = TRUE in the function improve.kppm:
Error in improve.kppm(object, type = type, rmax = rmax, dimyx = dimyx, :
object 'gminus1' not found
If I don't include the argument, the function runs but I cannot use the summary() function on the improved kppm object. I get the same error message as above. The same thing happens when I use vcov().
The call that I used to create my kppm object was (number of covariates has been reduced for clarity):
a05 = kppm(a2005nests ~ nest + nest2, cluster = "Thomas", covariates = fitcov(2))
where fitcov(2) is a function that returns a list of im objects. Could this be the issue? I've noticed that some spatstat functions on kppm objects throw errors if I used this function in the original kppm call. Usually it says something along the lines of Error: Covariates ‘nest’ and ‘nest2’ were not found.
There is a bug in the logical flow of improve.kppm: if vcov=TRUE and type != "quasi", the variable gminus1 is not defined. We will fix this in the development version of spatstat as soon as possible.
Did you perhaps select type="clik1" or type="wclik1" in the original call to kppm?
For the moment, you should be able to avoid the bug by omitting the argument type, or explicitly selecting type="quasi", in calls to kppm and improve.kppm.
The second problem, in which kppm fails to find the covariates, appears to be a scoping problem, but I can't reproduce it here. It would help if you can supply a minimal working example.

How to specify additional parameters in R functions?

For Fitting Markov Switching Models with package MSwM (function msmFit) there is 'control' argument which is list of control parameters.
Syntax of msmFit is:
msmFit(object, k, sw, p, data, family, control)
The 'control' argument is a list that can supply any of the following components:
-trace: A logical value. If it is TRUE, tracing information on the progress of the optimization is produced.
-maxiter: The maximum number of iterations in the EM method. Default is 100.
and so on.
My question is how to specify for example '-maxiter'? I tried: component(maxiter=50), component-maxiter=50, component[["maxiter"]]=50. Everything gives an error. "unexpected '='" or other errors connected to argument.
Format your call something like this:
mod=msmFit(model,k=2,sw=rep(TRUE,8),control=list(maxiter=10))

Error in cv.glmnet

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.

Resources