r - Non-standard evaluation with fitting a distribution to truncated data
问题描述
I would like to create a function that fits a gamma distribution to truncated data.
Let's say I have the following truncated data:
# Simulate some truncated data from gamma distribution
library(truncdist)
library(fitdistrplus)
set.seed(1)
e <- rgamma(1000, scale = 148099.5, shape = 0.4887023)
left_tr <- 30000
f <- e[e > left_tr] # truncated data
Then following Fitting a lognormal distribution to truncated data in R, I would fit a gamma distribution to the truncated data.
# Fitting a gamma distribution to truncated data
fit_gamma <- function(y, left = NULL, right = NULL){
dtruncated_gamma <- function(x, scale, shape) {
dtrunc(x, "gamma", a = left, b = right, scale = scale, shape = shape)
}
ptruncated_gamma <- function(q, scale, shape) {
ptrunc(q, "gamma", a = left, b = right, scale = scale, shape = shape)
}
fitdistrplus::fitdist(y, "truncated_gamma",
method = "mle",
lower = c(0,0),
start = list(scale = 149919.5, shape = 0.8955081))
}
fit_gamma()
returns an error:
fit_gamma(f, left = 30000, right = Inf)
#> Error in fitdistrplus::fitdist(y, "truncated_gamma", method = "mle", lower = c(0, : The dtruncated_gamma function must be defined
What is the way to solve this error?
Created on 2021-10-25 by the reprex package (v2.0.1)
解决方案
推荐阅读
- javascript - 如何区分网站上的每个设备
- visual-studio-code - 如何在注释和字符串中使用片段
- java - onClickListener 不适用于卡片视图
- expect - 期待下一次产生不正确的用户名或密码
- sql - 连接如何影响使用的索引?
- python - 用于嵌套自定义对象的石墨烯解析器
- angular - Angular 7 + ShadowDOM - @angular/material 皮肤的加载策略
- html - 将动态 HTML 表格传递给电子表格
- javascript - 如何复制 HTML 和用户输入,然后通过 HTTP 请求将其发送到服务器?
- javascript - Bootstrap4 JQuery 响应选项卡工作不正确,提供了片段