错误F(X,...):参数“X”的缺失,在NLM没有默认(Error in f(x, …) : ar

2019-10-29 21:44发布

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”的缺失,没有默认设置帮我找出错误和解决方案来解决这个问题了

Answer 1:

传递给函数的参数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


文章来源: Error in f(x, …) : argument “x” is missing, with no default in nlm
标签: r nlm