简体   繁体   English

从 R 中的公式对象中删除“公式”类

[英]Remove “Formula” class from formula object in R

I use the package Formula in R to create a conditional formula that can be updated.我使用 R 中的包Formula来创建一个可以更新的条件公式。

(described here: How to update a conditional formula? ) (此处描述: 如何更新条件公式?

By using the Formula package, my formula object gets two classes: "Formula" and "formula" .通过使用Formula包,我的公式对象获得两个类: "Formula""formula" Unfortunately, the "Formula" class does not work with the FENmlm package that I use for calculating my model.不幸的是, "Formula"类不适用于我用于计算模型的FENmlm包。 Is there a way to remove the "Formula" class and keep the "formula" class?有没有办法删除"Formula"类并保留"formula"类? (the "Formula" class is only needed for updating the models) "Formula"类仅用于更新模型)

# Conditional formula
fml1 <- Formula::Formula(Petal.Width ~ Petal.Length | Species)

# Update conditional formula
fml2 <- update(fml, . ~ . + Sepal.Length)

# Class
class(fml2)
# [1] "Formula" "formula"

# Calculation
FENmlm::femlm(fml2, data = iris)
# Error in FENmlm::femlm(fml2, data = iris) : 
# The argument 'fml' must be a formula.

# Try to delete "Formula" attribute
attributes(fml2)[[1]][[1]] <- NULL
# Error in attributes(fml2)[[1]][[1]] <- NULL : replacement has length zero

You can update the class of the formula with class <- -您可以使用class <- - 更新公式的class <-

fml1 <- Formula::Formula(Petal.Width ~ Petal.Length | Species)

# Update conditional formula
fml2 <- update(fml1, . ~ . + Sepal.Length)
class(fml2) <- 'formula'

FENmlm::femlm(fml2, data = iris)

#ML estimation, family = Poisson, Dep. Var.: Petal.Width
#Observations: 150 
#Cluster sizes: Species: 3
#Standard-errors type: Standard 
#              Estimate Std. Error   z value Pr(>|z|) 
#Petal.Length  0.150326   0.259850  0.578512 0.562919 
#Sepal.Length -0.012575   0.227379 -0.055302 0.955898 
#---
#Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#           BIC: -147.53      Pseudo-R2: 0.19402 
#Log-likelihood:  345.17   Squared Cor.: 0.94148 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM