用R语言绘制似然面Likelihood Surface

likelihood即为各种概率密度的乘积

Draw Likelihood Surface using R

### 绘制似然面： Likelihood Surface

## Authorjinlongzhang01@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", ...) axis(1, x$xaxis); axis(2, x\$yaxis); box()

}

### 以上函数拷贝到R中即可

### 应用举例

dat = c(-0.77, 6.50, 2.17, -0.51, 1.1, 2.52, 0.17, -0.01, 1.02, 0.11, -0.31, -0.12, 0.25, 0.84)

#dat = rnorm(10)

lks <- LikSurf(dat, precision = 0.05)

image(lks, col = cm.colors(20))

contour(lks, drawlabels = TRUE, add = TRUE)

http://blog.sciencenet.cn/blog-255662-509292.html

全部精选博文导读

GMT+8, 2018-10-23 18:40