0
rm(list=ls(all=TRUE))
data <- read.csv("con.csv", header=TRUE, sep = ",")
x <- data$X0
n = length(x); T1 <- 1
f <- function(a,b) {
  L <- (n*log(a))+(n*a*log(T1))+(n*a*log(b))-(n*log((T1^a)-(b^a)))- ((a+1)*sum(log(b+x)))
  return(-L)  
}

ML <- nlm(f, c(0.01,0.17))

导致 f(x, ...) 中的错误:缺少参数“x”,没有默认值帮助我找出错误和解决方案

4

1 回答 1

2

传递给函数的参数f必须是单个向量。这是正确的定义:

f <- function(pars) {
  L <- (n*log(pars[1]))+(n*pars[1]*log(T1))+(n*pars[1]*log(pars[2]))-
       (n*log((T1^pars[1])-(pars[2]^pars[1])))- ((pars[1]+1)*sum(log(pars[2]+x)))
  return(-L)  
}

和一个工作示例:

set.seed(1234)
n <- 100
x <- runif(n)+5
T1 <- 1
(ML <- nlm(f, p=c(0.01,0.17)))

结果是:

$minimum
[1] 227.3527

$estimate
[1] 2.420050e-07 1.768907e-01

$gradient
[1]  259.0327 -308.4809

$code
[1] 2

$iterations
[1] 12
于 2017-05-02T13:12:20.073 回答