Tweedie distribution error in R - r

I cannot debug the following Tweedie error in R. The tweedie function code has worked in other applications and I have checked the data. Any help would be appreciated.
glm1a <- glm(Incurred_Loss ~ operator_age_group + credit_group +
motor_length_group +stated_amount_group + Marine_Age_group,
family=tweedie(var.power=tweedie.p, link.power=0),
data=Tnwater1.df, offset=log(Exposure),
weights=Exposure^(tweedie.p - 1))
Error in if (!(validmu(mu) && valideta(eta))) stop("cannot find valid starting values: please specify some", :
missing value where TRUE/FALSE needed
In addition: Warning message:
In log(mu) : NaNs produced

Related

How do I fix a dissimilarities error in an adonis2 test in r?

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

glmmLasso R Error:Error in grad.lasso[b.is.0] <- score.beta[b.is.0] - lambda.b * sign(score.beta[b.is.0])

I am trying to use glmmLasso to run a multilevel logistic regression model, as I believe I am getting some wonky results due to sparse data bias. My outcome variable is a binary (0, 1) variable and my grouping variable is ID. Here is my code:
m1 <- glmmLasso(outcome ~ c.AGE + c.BSS + c.negative.emotion +
c.PSI_Total + c.MEPS_Ratio + c.OTT_Ratio + c.AAQ_Total +
c.BHS_Total, rnd=list(ID=~1 + c.negative.emotion),
lambda=100, data=data.set.3, family=binomial(link="logit"))
This is the error I am receiving:
Error in grad.lasso[b.is.0] <- score.beta[b.is.0] - lambda.b * sign(score.beta[b.is.0]) :
NAs are not allowed in subscripted assignments
In addition: Warning message:
In Ops.factor(y, Mu) : ‘-’ not meaningful for factors
I am unsure why I am getting this error. I did see another post about this error on StackOverflow but I was unable to use the fix for my data. There are no NAs in the dataset. I've attached the dataset in CSV format here.

I don't understand this mistake in R: In pt(-abs(tVal), fDF) : NaNs produced

I'm doing a mixed model with the function lme:
The code is:
modelomixto1<-lme(fixed= LargoCorola~Tratamiento*Rango, random = ~1|Rango/Poblacion/Codigo.FG,
na.action=na.exclude, data = df1)
I think the code is correct, but the result gives me a warnning message:
Warning message:
In pt(-abs(tVal), fDF) : NaNs produced
I'm not any expert in R, so I can't understand what is wrong.
Thanks in advance

polycor package - hetcor error in optim

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!

fitdistr from MASS library gives warnings when fitting t-distribution

I'm trying to reproduce the following example from David Ruppert's "Statistics and Data Analysis for Financial Engineering", which fits Students t-distribution to historical risk free rate:
library(MASS)
data(Capm, package = "Ecdat")
x <- Capm$rf
fitt <- fitdistr(x,"t", start = list(m=mean(x),s=sd(x)), df=3)
as.numeric(fitt$estimate)
0.437310595161651 0.152205764779349
The output is accompanied by the following Warnings message:
Warning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs producedWarning message:
In log(s): NaNs produced
It appears from the R's help file that MASS::fitdistr uses maximum-likelihood for finding optimal parameters. However, when I do optimization manually (same book), all goes smoothly and there is no warnings:
library(fGarch)
loglik_t <- function(beta) {sum( - dt((x - beta[1]) / beta[2],
beta[3], log = TRUE) + log(beta[2]) )}
start <- c(mean(x), sd(x), 5)
lower <- c(-1, 0.001, 1)
fit_t <- optim(start, loglik_t, hessian = T, method = "L-BFGS-B", lower = lower)
fit_t$par
0.44232633269102 0.163306955396773 4.12343777572566
The fitted parameters are within acceptable standard errors, and, in addition to mean and sd I have gotten df.
Can somebody advise me please:
Why MASS::fitdistr produces warnings whereas optimization via fGarch::optim succeeds without a warning?
Why there is no df in MASS::fitdistr output?
Is there a way to run MASS:fitdistr on this data without a warning and get df?
Disclaimer:
a similar question was asked couple of times without an answer here and here
You are not passing the lower argument to the function fitdistr which leads it to make a search in positive and negative domain. By passing the lower argument to function
fitt <- fitdistr(x,"t", start = list(m=mean(x),s=sd(x)), df=3, lower=c(-1, 0.001))
you get no NaNs -as you did in your manual optimisation.
EDIT:
fitt <- fitdistr(x,"t", start = list(m=mean(x),s=sd(x),df=3),lower=c(-1, 0.001,1))
returns non-integer degrees of freedom result. However, I guess, the rounded value of it, which is round(fitt$estimate['df'],0) can be used for fitted degrees of freedom parameter.

Resources