如何从 R 中的模型中获取系数?

How to get coefficients from a model in R?

我正在尝试存储来自 mle 模型的系数,就像通常对其他模型所做的那样。令人惊讶的是,它抛出了一个错误。还有其他存储方法吗?

set.seed(111)
y.size <- rnorm(100,4,1)
p <- rnorm(100,5,1)

df <- data.frame(p, y.size)

glae <- function(A,K,Ka,q, sigma, c) {
  lnqp <- if(q == 0) log(p) else ((p^q - 1)) / q
  y.pred <- ((A *((p*K/Ka)-1))-1)* lnqp + c
  ll <-   -sum(dnorm(y.size, mean = y.pred, sd = sigma, log=TRUE ))
  ll
}

mle2.model <- mle(glae, start = list(A = -2, K = 100, Ka = 200, q= 0.1, sigma = 0.1, c=3.8))
summary(mle2.model)

Maximum likelihood estimation

Call:

mle(minuslogl = glae, start = list(A = -2, K = 100, Ka = 200, q = 0.1,西格玛 = 0.1,c = 3.8))

Coefficients:
        Estimate Std. Error
A      12.511258        NaN
K      99.537509        NaN
Ka    200.231236 1684.30918
q     -32.959393        NaN
sigma 363.906677        NaN
c       8.135369   35.47982

-2 log L: 1363.185 


A.final <- summary(mle2.model)$Coefficients[1]

`Error in summary(mle2.model)$Coefficients : $ operator not defined for this S4 class

此外:警告消息:在 sqrt(diag(object@vcov)) 中:产生了 NaNs`

您可以使用 @ 运算符访问此“mle”S4 对象内容,例如

library(bbmle)
#> Loading required package: stats4

set.seed(111)
y.size <- rnorm(100,4,1)
p <- rnorm(100,5,1)

df <- data.frame(p, y.size)

glae <- function(A,K,Ka,q, sigma, c) {
  lnqp <- if(q == 0) log(p) else ((p^q - 1)) / q
  y.pred <- ((A *((p*K/Ka)-1))-1)* lnqp + c
  ll <-   -sum(dnorm(y.size, mean = y.pred, sd = sigma, log=TRUE ))
  ll
}

mle2.model <- mle(glae, start = list(A = -2, K = 100, Ka = 200, q= 0.1, sigma = 0.1, c=3.8))
coefs <- mle2.model@coef
coefs
#>          A          K         Ka          q      sigma          c 
#>  12.511258  99.537509 200.231236 -32.959393 363.906677   8.135369

reprex package (v2.0.1)

于 2022-05-27 创建