How to write the equations in R

I would like to ask how I can write the following equations in rstudio.

I think you would use MathJax

Is your question "how do I write these equations as R code?" or "How do I write and render these equations as they appear in the image?"

I want Rstudio code to compute these equations

How much R do you know ?
Can you write a function?
Are you familiar with loops?

Also, do you understand what these equations say to do ? Understanding of the equations will be important if you were to program them in R.

Please explain what the I(y = 0) means.

It means the sum of zeros in the data set

I'll do equations 1 and 2. In equation 2, for some reason this system is omitting some of my asterisks. In equation 3, I don't know what pi_o is.

# given data -  I just made up this data
y <- c(1,2,0,4,5,0)  
lambda <- 5
theta <- 2

# preliminary calculations
n_o <- length(which(y == 0))
A <- sum(y)
n <- length(y)

# equations
f1 <- -n + lambda + A*log(lambda) - sum(log(factorial(y)))
f2 <- n_o + log(pi + (1 - pi)*exp(-theta)) + (n - n_o)*log(1 - pi) - (n - n_o)* theta + A*log(theta) - sum(log(factorial(y)))

Let me add one very small twist to @fcas80's solution. Rather than log(factorial(y)) use lfactorial(y). The reason is that for values of y around 200 or larger, factorial explodes (returns Inf) where lfactorial works okay.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.