Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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_Statistics_Probability Density_Beta Distribution - Fatal编程技术网

在R中绘制β分布图

在R中绘制β分布图,r,ggplot2,statistics,probability-density,beta-distribution,R,Ggplot2,Statistics,Probability Density,Beta Distribution,使用数据集Lahman::Batting我估计了β分布的参数。现在我想将这个经验得出的贝塔分布绘制到我根据估计的直方图上 library(dplyr) library(tidyr) library(Lahman) career <- Batting %>% filter(AB > 0) %>% anti_join(Pitching, by = "playerID") %>% group_by(playerID) %>% summarize(H

使用数据集
Lahman::Batting
我估计了β分布的参数。现在我想将这个经验得出的贝塔分布绘制到我根据估计的直方图上

library(dplyr)
library(tidyr)
library(Lahman)

career <- Batting %>%
  filter(AB > 0) %>%
  anti_join(Pitching, by = "playerID") %>%
  group_by(playerID) %>%
  summarize(H = sum(H), AB = sum(AB)) %>%
  mutate(average = H / AB)
并获得:

我知道我可以使用
+geom\u freqpoly
获得:

但是我想要平滑的beta分布。我可以通过以下方式估计beta参数:

career_filtered <- career %>%
    filter(AB >= 500)

m <- MASS::fitdistr(career_filtered$average, dbeta,
                    start = list(shape1 = 1, shape2 = 10))

alpha0 <- m$estimate[1] # parameter 1
beta0 <- m$estimate[2] # parameter 2
career\u已筛选%
过滤器(AB>=500)

m可以找到所有代码,包括绘图代码。以下代码用于获取请求的绘图:

ggplot(career_filtered) +
  geom_histogram(aes(average, y = ..density..), binwidth = .005) +
  stat_function(fun = function(x) dbeta(x, alpha0, beta0), color = "red",
                size = 1) +
  xlab("Batting average")

希望这能有所帮助。

也许,您可以通过@BenBolker查看。谢谢Florian——这很有效!我知道你也读过这篇关于经验贝叶斯估计的文章。非常强大但简单的方法。。。
ggplot(career_filtered) +
  geom_histogram(aes(average, y = ..density..), binwidth = .005) +
  stat_function(fun = function(x) dbeta(x, alpha0, beta0), color = "red",
                size = 1) +
  xlab("Batting average")