-
Notifications
You must be signed in to change notification settings - Fork 1
/
loglik.R
26 lines (26 loc) · 1 KB
/
loglik.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#' Log-likelihood
#'
#' This function calculates the log-likelihood at some parameter values given the random sample and distribution.
#'
#' @param x numeric vector.
#' @param dist density name for the distribution in the form \code{dxxx}. By default it's value is \code{dnorm} but the user can use any density.
#' @param param list with the parameter values at which can be calculated the log-likelihood.
#'
#' @examples
#' # For Poisson distribution
#' x <- rpois(n=50, lambda=4.5)
#' loglik(x, dist='dpois', param=list(lambda=4))
#' loglik(x, dist='dpois', param=list(lamb=4.5))
#' loglik(x, dist='dpois', param=list(lambda=5))
#'
#' # For normal distribution
#' y <- rnorm(n=100, mean=170, sd=10)
#' loglik(x=y, dist='dnorm', param=list(mean=170, sd=10)) # Evaluated at true theta
#' loglik(x=y, dist='dnorm', param=list(mean=150, sd=20)) # Evaluated at any theta
#'
#' @export
#'
loglik <- function(x, dist='dnorm', param=list(mean=0, sd=1)) {
ll <- do.call(dist, c(list(x=x), param, log=TRUE))
return(sum(ll))
}