Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将边缘分布拟合到R中直方图的示例_R_Ggplot2_Histogram_Distribution_Bayesian - Fatal编程技术网

将边缘分布拟合到R中直方图的示例

将边缘分布拟合到R中直方图的示例,r,ggplot2,histogram,distribution,bayesian,R,Ggplot2,Histogram,Distribution,Bayesian,有人能告诉我如何使多项式边际分布适合我的数据吗?我已经做了二项式和贝塔二项式,但我想看看如何拟合多项式。如果你知道怎么做的话,我也有兴趣尝试伽马射线 这就是我到目前为止所做的 nodes <- read.table("https://web.stanford.edu/~hastie/CASI_files/DATA/nodes.txt", header = T) nodes %>% ggplot(aes(x=x/n))+ geom_histogram(bi

有人能告诉我如何使多项式边际分布适合我的数据吗?我已经做了二项式和贝塔二项式,但我想看看如何拟合多项式。如果你知道怎么做的话,我也有兴趣尝试伽马射线

这就是我到目前为止所做的

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")
节点%
ggplot(aes(x=x/n))+
geom_直方图(箱数=30)+
主题_bw()+
实验室(x=“节点”,
n=“p=x/n”)
#对数似然函数

ll用于拟合伽马分布:

data(iris)
library(MASS) ##for the fitdistr function

fit.params <- fitdistr(iris$Sepal.Length, "gamma", lower = c(0, 0))

ggplot(data = iris) + 
 geom_histogram(data = as.data.frame(x), aes(x=iris$Sepal.Length, y=..density..)) +
 geom_line(aes(x=iris$Sepal.Length, 
 y=dgamma(iris$Sepal.Length,fit.params$estimate["shape"], 
 fit.params$estimate["rate"])), color="red", size = 1) + 
 theme_classic()
数据(iris)
fitdistr函数的库(MASS)##

fit.params如何拟合自定义多项式?FitDisr允许吗?你能解释一下你的目标是什么吗?我从来没有听说过将自定义多项式拟合到直方图。如果您想拟合自定义多项式回归,可以使用lm()函数。是的,我正在尝试复制上一篇文章中的绘图,但我仍然不确定如何进行,请参见此处,感谢链接到更多详细信息。我花了一些时间来玩弄这个,恐怕我不知道还有什么好尝试的。也许其他人会插嘴。祝你好运。
data(iris)
library(MASS) ##for the fitdistr function

fit.params <- fitdistr(iris$Sepal.Length, "gamma", lower = c(0, 0))

ggplot(data = iris) + 
 geom_histogram(data = as.data.frame(x), aes(x=iris$Sepal.Length, y=..density..)) +
 geom_line(aes(x=iris$Sepal.Length, 
 y=dgamma(iris$Sepal.Length,fit.params$estimate["shape"], 
 fit.params$estimate["rate"])), color="red", size = 1) + 
 theme_classic()
library(car)
qqp(iris$Sepal.Length, "norm") ##normal distribution

qqp(iris$Sepal.Length, "lnorm") ##log-normal distribution

gamma <- fitdistr(iris$Sepal.Length, "gamma")
qqp(iris$Sepal.Length, "gamma", shape = gamma$estimate[[1]], 
 rate = gamma$estimate[[2]]) ##gamma distribution

nbinom <- fitdistr(iris$Sepal.Length, "Negative Binomial")
qqp(iris$Sepal.Length, "nbinom", size = nbinom$estimate[[1]], 
 mu = nbinom$estimate[[2]]) ##negative binomial distribution