2017-07-26 6 views
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") 

enter image description here

ガンマ分布を装着するための別のフィット

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") 

enter image description here

答えて

1

です:

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() 

あなたは可能性ALS o車のパッケージのqqp関数を使って分位数の分布を調べるのが好きです。いくつか例を示します:

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 

ggplotsまたはqqPlotsにはfitdistr関数を使用できます。さまざまなディストリビューションをサポートしています。どうしたらよいでしょうか?fitdistr

+0

カスタム多項式はどのようにフィットしますか? fitdistrはそれを許しますか? – Alex

+1

ここであなたの目標が何であるかをもう少し説明できますか?私はちょうどヒストグラムにカスタム多項式を当てはめたことは聞いたことがありません。カスタム多項式回帰に適合させたい場合は、lm()関数を使用できます。 – Jay

+0

はい、私は前の投稿からプロットを複製しようとしていますが、私はまだそれを行う方法がわかりません。https://stackoverflow.com/questions/45290265/reproduce-a-prior-density-plot-in -r – Alex

関連する問題