简体   繁体   中英

Fitting a weighted distribution in R

I'm looking to fit a weighted distribution to a data set I have.

I'm currently using the fitdist command but don't know if there is a way to add weighting.

library(fitdistrplus)
df<-data.frame(value=rlnorm(100,1,0.5),weight=runif(100,0,2))

#This is what I'm doing but not really what I want
fit_df<-fitdist(df$value,"lnorm")

#How to do this
fit_df_weighted<-fitdist(df$value,"lnorm",weight=df$weight)

I'm sure this has been answered before somewhere but I've looked and can't find anything.

thanks in advance,

Gordon

Perhaps you could use the rep() function and a quick loop to approximate the distribution.

You could multiply each weighted value by, say, 10000, round the number, and then use it to indicate how many multiples of the value you need in your vector. After running a quick loop, you could then run the vector through the fitdist() algorithm.

df$scaled_weight <- round(df$weight*10000,0)
my_vector <- vector()

## quick loop
for (i in 1:nrow(df)){
  values <- rep(df$value[i], df$scaled_weight[i])
  my_vector <- c(my_vector, values)
}

## find parameters
fit_df_weighted <- fitdist(my_vector,"lnorm")

The standard errors would be rubbish, but the estimated parameters should be sufficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM