如何绘制涉及求和的函数并记录R

时间:2016-01-26 19:10:56

标签: r

如何在R中绘制以下函数

$ l(\ theta)= ln(\ theta)* \ sum {y_i} -n * \ theta -n * ln(1-e ^ { - \ theta}) - \ sum {ln(y_i!) } $

其中汇总是从$ i = 1 $到$ n $

我有数据集,但我知道如何输入。

1 个答案:

答案 0 :(得分:0)

这样的事情:

L <- function(theta, y){ 
         log(theta)*sum(y) - 
         length(y) *(theta + log(1 - exp(-theta))) - 
         sum(lfactorial(y))
         }
L_theta <- function(theta){
         L(theta, y)
         }
#example
y <- c(1, 3, 5, 7)
L(2, y)
# [1] -11.4324
plot(L_theta, xlim=c(0, 2), xlab="θ" )

产生类似

的东西

enter image description here