1
有人可以告诉我如何将多项式边际分布拟合到我的数据中吗?我做了二项和二项二项式,但我想看看如何拟合一个多项式。如果这是你知道该怎么做的,我也会对尝试伽玛感兴趣。将边缘分布拟合到直方图中的示例R
这是我迄今为止所做的。
nodes <- read.table("https://web.stanford.edu/~hastie/CASI_files/DATA/nodes.txt",
header = T)
nodes %>%
ggplot(aes(x=x/n))+
geom_histogram(bins = 30)+
theme_bw()+
labs(x = "nodes",
n = "p=x/n")
# log-likelihood function
ll <- function(alpha, beta) {
x <- nodes$x
total <- nodes$n
-sum(VGAM::dbetabinom.ab(x, total, alpha, beta, log = TRUE))
}
# maximum likelihood estimation
m <- mle(ll, start = list(alpha = 1, beta = 10), method = "L-BFGS-B",
lower = c(0.0001, .1))
ab <- coef(m)
alpha0 <- ab[1]
beta0 <- ab[2]
nodes %>%
ggplot() +
geom_histogram(aes(x/n, y = ..density..), bins= 30) +
stat_function(fun = function(x) dbeta(x, alpha0, beta0), color = "red",
size = 1) +
xlab("p=x/n")
这里是另一个适合
ll <- function(a){
x <- nodes$x
total <- nodes$n
-sum(stats::dbinom(x, total, a, log = TRUE))
}
#stats::dbinom()
m <- mle(ll, start = list(a=.5), method = "L-BFGS-B",
lower = c(0.0001, .1))
a = coef(m)
nodes %>%
ggplot() +
geom_histogram(aes(x/n, y = ..density..), bins=40) +
stat_function(fun = function(x) dbeta(x, a, 1), color = "red",
size = 1) +
xlab("proportion x/n")
我将如何适应自定义多项式? fitdistr允许吗? – Alex
你能否更详细地解释一下你的目标是什么?我从来没有听说过将自定义多项式拟合到直方图上。如果要拟合自定义多项式回归,可以使用lm()函数。 – Jay
是的,我试图从以前的帖子复制情节,但我仍然不确定如何去做,请看这里https://stackoverflow.com/questions/45290265/reproduce-a-prior-density-plot-in -r – Alex