当前位置:天才代写 > tutorial > 其他教程 > 用R语言绘制似然面Likelihood Surface

用R语言绘制似然面Likelihood Surface

2017-12-05 08:00 星期二 所属: 其他教程 浏览:391

本文的目标,是直观表达极大似然预计。给出一组数,若用正态漫衍拟合,则其平均值和尺度差最大概的取值是什么?
极大似然面的极点,就是最优预计。实际环境下,是对对数似然函数Log
Likelihood取极值。这里只凭据似然(Likelihood)的原始界说,直接求Likelihood

likelihood即为各类概率密度的乘积
留意R code 中的

prod(dnorm(dat, mean_val[i], sd_val[j]))

以下是绘制拟合正态漫衍的似然面。

Draw Likelihood
Surface using R

###
绘制似然面: Likelihood Surface

##
Author:jinlongzhang01@gmail.com

LikSurf
<-
function(dat, precision = 0.05){

mean_val <- seq(min(dat), sqrt(max(dat)), by = precision)

sd_val <- seq(0, exp(sd(dat)), by = precision)

mat <- rep(NA, length(mean_val)*length(sd_val))

dim(mat) <- c(length(mean_val), length(sd_val))

for(i in 1:length(mean_val)){

for(j in 1:length(sd_val)){

mat[i, j] <- prod(dnorm(dat, mean_val[i], sd_val[j]))

}

}

#contour(mat)

res <- list(xaxis = axisTicks(range(mean_val), log = FALSE),

yaxis = axisTicks(range(sd_val), log = FALSE),

mean_val = mean_val, sd_val = sd_val,

mat = mat, pre = precision)

class(res) <- “LikSurf”

return(invisible(res))

}

print.LikSurf
<- function(x){

cat(“This is a likelihood surface
with”,
“\nx_range: “,

range(x$xaxis), “\ny_range:”, range(x$yaxis),

“\nprecision”, x$pre, “\n”)

}

contour.LikSurf
<- function(x, …){

contour(x = x$mean_val, y = x$sd_val, z = x$mat, axes = FALSE, xlab = “Mean”, ylab = “sd”, …)

axis(1, x$xaxis); axis(2, x$yaxis); box()

}

image.LikSurf
<- function(x, …){

image(x = x$mean_val, y = x$sd_val, z = x$mat, axes = FALSE, xlab = “Mean”, ylab = “sd”, …)

<

 

    关键字:

天才代写-代写联系方式