R programming language mmedist The moment function must be defined. fdistrplus package - r

I'm trying to fit a distribution to some data using the function fdist from the package fdistrplus.
I've started learning R recently and got a hold of this package that I thought could do everything I needed.
When trying to use it to fit to a pareto distribution, I had to input my definitions of the pareto distribution like so:
ppareto = function(x,alpha,theta){
(1-(theta/(x+theta))^alpha)*(x>=0)
}
dpareto = function(x,alpha,theta){
(alpha*theta^alpha*(x+theta)^(-alpha-1))*(x>=0)
}
rpareto = function(n,alpha,theta){
l = array(as.numeric(NA),n)
ltmp = runif(n,0,1)
l = teta/ltmp^(1/alpha)-theta
return(l)
}
When calling
fitdist(data,"pareto",method = "mme")
It said it could not find the "dpareto" function. That is resolved by the code above.
Afterwards it said it needed an order parameter and a empirical moment parameter.
It's my understanding that the order parameter is the vector of orders that I want the routine to use as it's basis for the calculation.
So I tried:
fitdist(data,"pareto",method="mme",order=c(1,2))
As I saw in the examples of the package pdf.
Then it complained that:
Error in mmedist(data, distname, start = start, fix.arg = fix.arg, ...) :
The moment function must be defined.
Okay, next step:
momentfunc = function(x,order){mean(x^order)}
To define the function.
Then I tried
fitdist(data,"pareto",method="mme",order=c(1,2),memp=momentfunc)
Error in mmedist(data, distname, start = start, fix.arg = fix.arg, ...) :
The moment function must be defined.
Then I tried:
fitdist(data,"pareto",method="mme",order=c(1,2),memp="momentfunc")
Error in mmedist(data, distname, start = start, fix.arg = fix.arg, ...) :
The moment function must be defined.
I've searched the web, found examples of the pareto dist and followed them step by step all to no avail...
I've searched stackoverflow and found nothing.
So here I am. If anyone could help me I would be much obliged.
Is it my dumb mistake? Is it a bug in the package?
I've tried this on Windows 7, R 3.1.0
and on MacOS, R 3.0.1 (where I get a warning that the package was built under R 3.0.2)
Thanks in advance

you have to install and load the 'actuar' package

Related

Error in setDefaultClusterOptions(type = .sfOption$type) :could not find function "setDefaultClusterOptions"

I'm new here. I've been struggling with analysing some data with the BaSTA package, the data works ok after running the "Datacheck" code , but right after running the following code this happens:
multiout <- multibasta(object = datosJ, studyStart = 1999, studyEnd = 2018, model = "LO",
shape = "simple", niter = 20001, burnin = 2001, thinning = 100,
parallel = TRUE)
No problems were detected with the data.
Starting simulation to find jump sd's... done.
Multiple simulations started...
**Error in setDefaultClusterOptions(type = .sfOption$type) :
could not find function "setDefaultClusterOptions"**
I believe this error has something to do with the usage of "parallel = TRUE" which is a function of the snow package that comes incorporated in the BaSTA package and makes the analysis run faster. If I don't use parallel the analysis takes weeks in running and I've been told that's not normal for the package I'm using.
Any help would be very helpful, thank you.
I came across this same behavior when using another R package that depends on snowfall. setDefaultClusterOptions is housed within a dependency of BaSTA so this is error message is because packages are not being loaded. Try calling library(snowfall) prior to running the BaSTA package command to see if that fixes it for you.

R - Alternatives to, or fix for, LMERConvenienceFunctions, for use with LME4?

I am crossposting this from https://stats.stackexchange.com/questions/488201/alternatives-to-or-fix-for-lmerconveniencefunctions-for-use-with-lme4 as it was suggested that people might have more relevant knowledge here.
Recently I updated from R version 3.6 to version 4.0 for my analyses, and noticed that LMERConvenienceFunctions stopped working. Specifically, I use it in conjunction with LME4.
Whenever I try to use the bfFixefLMER_F.fnc (backfitting of fixed effects in LMER models) or pamer.fnc (compute upper- and lower-bound p-values for the analysis of variance or deviance) for a LMER model fitted through LME4, regardless of dataset, I am met with the error "Error in pf(anova.table[term, "F value"], anova.table[term, "Df"], nrow(model#frame) - : Non-numeric argument to mathematical function". I have tried this on two separate computers with the same result. Now, as far as I can tell, LMERConvenienceFunctions hasn't been updated since 2015, so I'm not holding out hope that a fix is forthcoming.
I tried reverting to R 3.6.2, but found the same error using the versions of LME4 that were out shortly before R 4.0 came out. I have finally found the previous version I was using, so this will (hopefully) fix it for my current analysis, but doesn't help if I want to use the most recent version of R and LME4 going forwards.
Other functions of LMERConvenience (namely fitLMER.fnc and mcp.fnc) seem to be working properly, so it doesn't seem to be a systematic issue, but it is definitely one that significantly impedes my work.
Does anyone have any suggestions for alternative packages, or could anyone offer any advice on editing the LMERConvenienceFunctions package so that I can get the broken functions working again? I don't have any experience with changing the coding within packages, so would be starting from bare basics there.
I am also aware that there are some workarounds through adding extra code in my R script, as I did find in my searching for an answer that it had previously been a problem with the same package in 2014 (https://stat.ethz.ch/pipermail/r-sig-mixed-models/2014q2/022264.html), but I am not familiar with writing that kind of code, so would appreciate any guidance there as well.
I managed to get some help from the lovely people working with LME4 on github; for anyone who has this issue in the future, there are a few things of note:
I have emailed the listed maintainer of LMERConvenienceFunctions asking for them to update the CRAN version so that it abides by the rules of CRAN. Hopefully he does this.
For a fix for those using LME4 and LMERConvenienceFunctions, but who are NOT also using lmerTest, recently the anova table headers "Chi Df" and"Df" were updated to "Df" and "npar" respectively (reasons outlined here:https://github.com/lme4/lme4/issues/528). This is an issue for LMERConvenienceFunctions because pamer.fnc called to Df, so needs to be updated to call to npar instead. Further, bfFixefLMER_F.fnc calls to pamer.fnc, so is fixed when pamer.fnc is updated. For anyone unsure how, I found the code using getAnywhere() and modified it, so just copy the code below and paste it once right near the start of the file:
pamer.fnc <- function (model, ndigits = 4)
{
if (length(rownames(anova(model))) == 0) {
cat("nothing to evaluate: model has only an intercept.\n\n")
cat("printing model fixed effects:\n")
fixef(model)
}
else {
dims <- NULL
rank.X = qr(model#pp$X)$rank
anova.table = anova(model)
anova.table = cbind(anova.table, upper.den.df = nrow(model#frame) -
rank.X)
p.values.upper = as.numeric()
p.values.lower = as.numeric()
for (term in row.names(anova.table)) {
p.values.upper = c(p.values.upper, round(1 - pf(anova.table[term,
"F value"], anova.table[term, "npar"],
nrow(model#frame) - rank.X), ndigits))
model.ranef <- ranef(model)
lower.bound <- 0
for (i in 1:length(names(model.ranef))) {
dims <- dim(model.ranef[[i]])
lower.bound <- lower.bound + dims[1] * dims[2]
}
p.values.lower = c(p.values.lower, 1 - pf(anova.table[term,
"F value"], anova.table[term, "npar"],
nrow(model#frame) - rank.X - lower.bound))
}
dv <- gsub(" ", "", gsub("(.*)~.*",
"\\1", as.character(model#call)[2]))
ss.tot <- sum((model#frame[, dv] - mean(model#frame[,
dv]))^2)
aov.table <- as.data.frame(anova(model))
expl.dev <- vector("numeric")
for (i in rownames(aov.table)) {
expl.dev <- c(expl.dev, aov.table[i, 2]/ss.tot)
}
names(expl.dev) <- rownames(aov.table)
anova.table = round(cbind(anova.table, upper.p.val = p.values.upper,
lower.den.df = nrow(model#frame) - rank.X - lower.bound,
lower.p.val = p.values.lower, `expl.dev.(%)` = expl.dev *
100), ndigits)
return(anova.table)
}
}
(You may also need to run the function script for bfFixefLMER_F.fnc separately again, to let R know that bfFixefLMER_F.fnc should be calling from the updated version of pamer.fnc)
For a fix for those using LME4 and LMERConvenienceFunctions, but who ARE also using lmerTest, you will need to either 1) use numDF instead of npar, or 2) replace "anova.table = anova(model)" with "anova.table = anova(model, dff = "lme4") in versions of lmerTest 3.0 onwards. This appears to be due to the anova.lmerModLmerTest function (added in lmerTest 3.0) overwriting what the call to anova() does, with a Type III Satterthwaite analysis listed before lme4, leading to it being defaulted to when dff is not specified.

How to fix 'could not find function mvOutlier' in R

I am trying to remove outliers from my dataset using the mvOutliers function in MVN in R. However, upon executing the program, I get the error, could not find function "mvOutlier".
I have installed all the packages from MVN to mvoutlier. I have also imported MVN but also tried importing mvoutlier but still I am getting the same error.
I am using the following code for outlier removal
library(MVN)
M <- mvOutlier(data, method = "quan", label = TRUE)
Comb_Data=M$newData
I expect the Comb_Data to contain the outlier free dataset but all I am getting is the error:
Error in mvOutlier(dat, method = "quan", label = TRUE) : could not
find function "mvOutlier"
Any help in this matter would be appreciated.

Error in `freeParam` in fitting of normal copula [R]

I am using R version 3.3.2 and the package copula version 0.999-15 to evaluate the fitting of the normal copula to my data. My data and code are:
Data: https://www.dropbox.com/s/tdg8bfzmy4nd1dd/jumps.dat?dl=0
library(copula)
data <- read.csv(file="jumps.dat", head=F, sep="")
cop_model <- ellipCopula("normal", dim = 2)
m <- pobs(as.matrix(data))
fitCopula(cop_model, m, method = 'mpl')
After I run the code I receive the following error:
Error in `freeParam<-`(`*tmp*`, value = estimate) : the length of 'value' is not equal to the number of free parameters
Calls: fitCopula ... fitCopula.ml -> fitCopStart -> fitCopula.icor -> freeParam<-
Execution halted
I have no idea what is happening here. The fitting for Clayton and Gumbel is pretty fine. Searching for similar errors in the web, I have found nothing. Reading the documentation (https://www.rdocumentation.org/packages/copula/versions/0.999-15/topics/fitCopula?) for some specificity for ellipCopula, I have found an specific option for posDef, but it did not returns any solution at all.
Old question, but I found this, so will share my solution.
Try to run the following, this is a minimum working example:
library(copula)
print("-----------")
mycop <- ellipCopula("normal", dim=4)
data <- matrix(runif(400), nrow=4)
fitCopula(mycop, t(data))
print("-----------")
For me, this works fine if I open R and type in the lines one by one, but fails if I run as a script with Rscript. The solution is that you need library(methods) as well.
For some reason this worked with copula v0.999-v14, but was broken by v0.999-v16. Alas.

Debugging lmer function

I am trying to feed a hierarchical model with the lmer function from the lme4 package but lattely while trying new dataset for my code I encountered an error that occurs when the chol function is called from the monadicbase function:
"Debug location is approximate because source code is not available"
chol.default <- function(x, pivot = FALSE, LINPACK = FALSE, tol = -1, ...)
{
if (is.complex(x))
stop("complex matrices not permitted at present")
.Internal(La_chol(as.matrix(x), pivot, tol))
}
By searching within the lmer code on Github, I figured out that the chol function is called from the checkConv function within the package lme4.
My problem is that I do not understand why I get this message all the time with a specific dataset. Clearly, when I get the result at the end, it is incorrect and I don't know how to step inside the lmer code to start debugging that issue since I do not find the same arborescence at all inside my downloaded package and can't find where the used code is.
I sadly do not have a reproducible example for you but if you could give me some tips as to how to start to step in the R code of the lme4 package in debug mode that would be great !
UPDATE: showing the info I have on the error
The traceback is the following:
eval(expr, envir, enclos)
eval(substitute(browser(skipCalls = pos), list(pos = (length(sys.frames()) -
chol.default(Hessian)
chol(Hessian)

Resources