我有一个gls模型,在该模型中,我为模型分配了一个公式(来自另一个对象):
equation <- as.formula(aic.obj[row,'model'])
> equation
temp.avg ~ I(year - 1950)
mod1 <- gls(equation, data = dat)
> mod1
Generalized least squares fit by maximum likelihood
Model: equation
Data: dat
Log-likelihood: -2109.276,但是,,我不希望“模型”成为“等式”,而希望它本身就是“方程”!我该怎么做??
发布于 2016-03-04 19:12:18
这是非常标准的,就连lm也会这么做。一种方法:劫持print.gls函数
library('nlme')
(form <- follicles ~ sin(2*pi*Time) + cos(2*pi*Time))
# follicles ~ sin(2 * pi * Time) + cos(2 * pi * Time)
(fm1 <- gls(form, Ovary))
# Generalized least squares fit by REML
# Model: form
# Data: Ovary
# Log-restricted-likelihood: -898.434
#
# Coefficients:
# (Intercept) sin(2 * pi * Time) cos(2 * pi * Time)
# 12.2155822 -3.3396116 -0.8697358
#
# Degrees of freedom: 308 total; 305 residual
# Residual standard error: 4.486121
print.gls <- function(x, ...) {
x$call$model <- get(as.character(x$call$model))
nlme:::print.gls(x, ...)
}
fm1
# Generalized least squares fit by REML
# Model: follicles ~ sin(2 * pi * Time) + cos(2 * pi * Time)
# Data: Ovary
# Log-restricted-likelihood: -898.434
#
# Coefficients:
# (Intercept) sin(2 * pi * Time) cos(2 * pi * Time)
# 12.2155822 -3.3396116 -0.8697358
#
# Degrees of freedom: 308 total; 305 residual
# Residual standard error: 4.486121 发布于 2016-03-04 19:28:50
你可以用一些巧妙的语言破坏来解决这个问题。这将使用直接插入的模型方程创建(未评估的) gls调用,然后计算它。
cl <- substitute(gls(.equation, data=dat), list(.equation=equation))
mod1 <- eval(cl)https://stackoverflow.com/questions/35803899
复制相似问题