rdistributionbeta-distribution

Fitting beta distribution with 0 in R


We have a dataset d3.

time MV
1 0
2 0
3 0,3

ect. with more data points between 0 to 1. We wanted to fit a beta distribution, but we found out that we should try to fit an zero inflated beta distribution since our data contains zeros.

But we do not really know how to do this. With this distribution we are first trying to fit a line over our data in a scatter plot if possible. Can somebody maybe give us a nudge in the right direction? we already found the package gamlss but don't know how to implement this the right way Thank you, 2 struggling students


Solution

  • Store your data in a vector. Here we use example data generated from rBEZI (random Beta Zero-Inflated) stored in x with mu=.5, sigma=1, and nu=.1 of size=10000. Now fit the data using the formula x~1. Specifying the starting values is optional if you don't have any idea what they could be, but here we knew what they were so it was okay to specify the start values to be their actual values. Don't forget to specify the family to be "BEZI" and you can access the fitted coefficients using fitted. Lastly, you can plot the data and the fitted curve to see if they're a good match.

    library(gamlss)
    x=rBEZI(10000, .5, 1, .1)
    m=gamlss(x~1, mu.start=.5, sigma.start=1, nu.start=.1, family="BEZI")
    fitted(m, "mu")[1]
    
    0.5015543
    
    fitted(m, "sigma")[1]
    1.008976 
    
    fitted(m, "nu")[1]
    0.0997
    
    plotBEZI(fitted(m, "mu")[1], fitted(m, "sigma")[1], fitted(m, "nu")[1])
    hist(x, freq=FALSE, add=TRUE)
    

    enter image description here