这是一种方法
获取数据的直方图
bl <- c(7.1, 2.3, 9.6, 25.8, 14.6, 12.8, 20.9, 30.0, 71.1, 36.9, 3.9, 18.2, 24.3, 55.8, 84.7,
13.6, 10.8, 18.2, 19.0, 34.8, 27.1, 54.2, 34.8, 65.8, 33.3, 26.5, 9.5, 44.2, 35.7, 2.3,
4.0, 25.9, 41.3)
b1h <- hist(bl, freq = FALSE, xlim = c(0, quantile(bl, 0.999)), plot=TRUE)
df <- data.frame(density=b1h$density, mids=b1h$mids)
将直方图绘制为点
plot(b1h$mids,b1h$density,cex=1,xlab='Life (hr)',ylab='Probability')
将指数分布拟合到直方图
fit <- nls(density ~ lambda*exp(-lambda*mids), start=list(lambda=.1), data=df)
lines(df$mids,predict(fit),col='red')
或者,将指数分布拟合到数据
fit1 <- fitdistr(bl, "exponential")
curve(dexp(x, rate = fit1$estimate), col = "green", add = TRUE)
平均寿命为1/λ,死亡率为λ
cat('from nls (red) the mean life is',1/coef(fit),'and mortality rate is',coef(fit),'per year')
cat('from fitdistr (green) the mean life is',1/coef(fit1),'and mortality rate is',coef(fit1),'per year')
结果是
from nls (red) the mean life is 35.47136 and mortality rate is 0.02819176 per year
from fitdistr (green) the mean life is 27.84848 and mortality rate is 0.0359086 per year

函数 fitdisr 提供参数的最大似然估计。nls 函数提供最小二乘估计。注意,对于指数分布,最大似然估计由 n/sum(xi) 给出,参考推导