Poisson distribution in R mixdist package - r

I think my data might be described by a sum of Poisson distributions and I found out about mixdist package for R. I managed to fit gamma and lnorm distributions, but I can't figure out how to use Poisson. I tried with the example data first:
library(mixdist)
data(poisdat)
data(poispar)
plot.mixdata(poisdat) #this works
fitp<-mix(poisdat, coef(poispar), "pois") #this doesn't
but I get an error Error in if (usecondit & ncol(mixdat) - 2 != k) stop("Conditional data are not consistent with mixpar.") : argument is of length zero
How to get a working example of mixdist and Poisson?

Are you looking for this:
mix(poisdat, poispar, "pois", constr = mixconstr(consigma = "POIS"))
Simply mix(poisdat, poispar, "pois") gives an Error:
Error in testconstr(mixdat, mixpar, dist, constr) :
Poisson needs consigma = POIS.
thas why this constr parameter.

Related

Unused argument error when building a Confusion Matrix in R

I am currently trying to run Logistic Regression model on my DF.
While I was creating a new modelframe with the actual and predicted values i get get the following error message.
Error
Error in confusionMatrix(as.factor(log_class), lgtest$Satisfaction, positive = "satisfied") :
unused argument (positive = "satisfied")
This is my model:
#### Logistic regression model
log_model = glm(Satisfaction~., data = lgtrain, family = "binomial")
summary(log_model)
log_preds = predict(log_model, lgtest[,1:22], type = "response")
head(log_preds)
log_class = array(c(99))
for (i in 1:length(log_preds)){
if(log_preds[i]>0.5){
log_class[i]="satisfied"}else{log_class[i]="neutral or dissatisfied"}}
### Creating a new modelframe containing the actual and predicted values.
log_result = data.frame(Actual = lgtest$Satisfaction, Prediction = log_class)
lgtest$Satisfaction = factor(lgtest$Satisfaction, c(1,0),labels=c("satisfied","neutral or dissatisfied"))
lgtest
confusionMatrix(log_class, log_preds, threshold = 0.5) ####this works
mr1 = confusionMatrix(as.factor(log_class),lgtest$Satisfaction, positive = "satisfied") ## this is the line that causes the error
I had same problem. I typed "?confusionMatrix" and take this output:
Help on topic 'confusionMatrix' was found in the following packages:
confusionMatrix
(in package InformationValue in library /home/beyza/R/x86_64-pc-linux-gnu-library/3.6)
Create a confusion matrix
(in package caret in library /home/beyza/R/x86_64-pc-linux-gnu-library/3.6)
Confusion Matrix
(in package ModelMetrics in library /home/beyza/R/x86_64-pc-linux-gnu-library/3.6)
As we can understand from here, since it is in more than one package, we need to specify which package we want to use.
So I typed code with "caret::confusionMatrix(...)" and it worked!
This is how we can write the code to get rid of argument error when building a confusion matrix in R
caret::confusionMatrix(
data = new_tree_predict$predicted,
reference = new_tree_predict$actual,
positive = "True"
)

error in coxph.wtest when using survreg and weibull in survival package, R

I am using survreg function and weibull distribution to predict the survival time for the last row of my data, But I got the error message.
My data is:
Train <- data.frame(Sum=c(2,2,2,2,2),
Days=c(21,21,21,21,21),
Status=c(1,1,1,1,1))
Test <- data.frame(Sum=2,
Days=21,
Status=1)
My code is:
FitWeibull <- survreg(Surv(Days,Status)~Sum,data=Train,dist='weibull')
PredictWeibull <- predict(FitWeibull,newdata=Test)
The error message is:
Error in coxph.wtest(t(x) %*% (wt * x), c((wt * eta + weights * deriv$dg) %*% :
NA/NaN/Inf in foreign function call (arg 3)
This data is just a small part of my whole data. All the other data runs well except for this one. It looks like this is because all the Days and Sum are the same. I change the Train to:
Train <- data.frame(Sum=c(2,2,2,2,2),
Days=c(21,21,21,21,20),
Status=c(1,1,1,1,1))
Then it is fine. So, weibull might cannot deal with all the same values. But I do not understand why this error happens and how to fix it?

Error when fitting a beta distribution: the function mle failed to estimate the parameters with error code 100

I'm trying to use fitdist () function from the fitdistrplus package to fit my data to different distributions. Let's say that my data looks like:
x = c (1.300000, 1.220000, 1.160000, 1.300000, 1.380000, 1.240000,
1.150000, 1.180000, 1.350000, 1.290000, 1.150000, 1.240000,
1.150000, 1.120000, 1.260000, 1.120000, 1.460000, 1.310000,
1.270000, 1.260000, 1.270000, 1.180000, 1.290000, 1.120000,
1.310000, 1.120000, 1.220000, 1.160000, 1.460000, 1.410000,
1.250000, 1.200000, 1.180000, 1.830000, 1.670000, 1.130000,
1.150000, 1.170000, 1.190000, 1.380000, 1.160000, 1.120000,
1.280000, 1.180000, 1.170000, 1.410000, 1.550000, 1.170000,
1.298701, 1.123595, 1.098901, 1.123595, 1.110000, 1.420000,
1.360000, 1.290000, 1.230000, 1.270000, 1.190000, 1.180000,
1.298701, 1.136364, 1.098901, 1.123595, 1.316900, 1.281800,
1.239400, 1.216989, 1.785077, 1.250800, 1.370000)
Next, if i run fitdist (x, "gamma") everything is fine, but if I use fitdist (x, "beta") instead I get the following error:
Error in start.arg.default(data10, distr = distname) :
values must be in [0-1] to fit a beta distribution
Ok, so I'm not native english but as far as I understand this method requires data to be in the range [0,1], so I scale it by using x_scaled = (x-min(x))/max(x). This gives me a vector with values in that range that perfectly correlates the original vector x.
Because of x_scaled is of class matrix, I convert into a numeric vector using as.numeric(). And then fit the model with fitdist(x_scale,"beta").
This time I get the following error:
Error in fitdist(x_scale, "beta") :
the function mle failed to estimate the parameters, with the error code 100
So after that I've been doing some search engine queries but I don't find anything useful. Does anybody ave an idea of whats going on wrong here? Thank you
By reading into the source code, it can be found that the default estimation method of fitdist is mle, which will call mledist from the same package, which will construct a negative log-likelihood for the distribution you have chosen and use optim or constrOptim to numerically minimize it. If there is anything wrong with the numerical optimization process, you get the error message you've got.
It seems like the error occurs because when x_scaled contains 0 or 1, there will be some problem in calculating the negative log-likelihood for beta distribution, so the numerical optimization method will simply broke. One dirty trick is to let x_scaled <- (x - min(x) + 0.001) / (max(x) - min(x) + 0.002), so there is no 0 nor 1 in x_scaled, and fitdist will work.

Error with Generalized portmanteau tests for garch [gBox] in R

I am trying to use the gBox() function from the TSA package in R. I want to test the goodness of fit for a GARCH model. But when I try to run the function I get this error message:
Error in filter(M, filter = beta, method = "recursive", sides = 1,
init = rep(sigma2,dims [product 2] do not match the length of object
[1]**
The funny thing is that this is from an exact replica of the example that the package instructions provide, so there really shouldn't be any errors one would think. I get the same error message for my own data as well and I just don't know what to do.
Here is the example code:
library(TSA)
library(tseries)
data(CREF)
r.cref=diff(log(CREF))*100
m1=garch(x=r.cref,order=c(1,1))
summary(m1)
gBox(m1,x=r.cref,method='squared')
The length of the time series r.cref is 500 and the length of the garch m1 is 10, so they're obviously not same length, but how do I fix this error?

Error with gamsel R Package

I'm trying to use the gamsel R package to fit a sparse generalized additive model, and I can't seem to get it to work on real data. When I run on synthetic data as described in the package documentation, everything works well:
library(gamsel)
data=gendata(n=500,p=12,k.lin=3,k.nonlin=3,deg=8,sigma=0.5)
attach(data)
bases=pseudo.bases(X,degree=10,df=6)
gamsel.out=gamsel(X,y,bases=bases)
But when I run on real data, I get the following error:
library(gamsel)
X = as.matrix(read.csv("X.csv"),header=FALSE)
y = as.matrix(read.csv("y.csv"),header=FALSE)
gam_fit = gamsel(X,y)
Error in if (abs((df - current.df)/df) < 1e-04 | iterations == 1)
return(list(lambda = lambda, : missing value where TRUE/FALSE
needed
You can access sample data files that will reproduce this result here. Any thoughts about how to fix this error?

Resources