在 R 中使用 VGAM::sm.ps 或 sm.ps 调用相同的函数会导致不同的输出

问题描述 投票:0回答:0

我正在尝试使用预测变量上的向量样条平滑器来拟合多项式逻辑回归模型。我在 R 中使用 VGAM 包(特别是 vgam 函数 来拟合模型)。我以前用过很多次,总是在公式中调用“sm.ps”来应用矢量样条平滑器。

但是,我正在尝试编写自己的包,其中包含 VGAM 作为导入。这意味着我需要使用

VGAM::
符号调用 VGAM 包中的函数。这样做后,我现在在拟合模型时得到的结果不正确。我已将问题确定为当我在函数公式中调用
VGAM::sm.ps(x1)
时,我得到的结果与调用
sm.ps(x1)
时不同。

要得到我遇到的确切问题需要共享大量代码,所以我创建了一个较小的可重现示例(如下)。不幸的是,这导致了另一个错误。在我的实际问题中,模型会拟合,但会返回错误的结果。在下面的可重现示例中,模型不适合,出现以下错误:

Error in vgam.fit(x = x, y = y, w = w, mf = mf, Xm2 = Xm2, Ym2 = Ym2,  : 
  vgam() only handles full-rank models (currently)

我希望诊断我的可重现示例有什么问题,仍然会解决我的实际问题。

### Load VGAM and backpain dataset
data("backPain")
str(backPain)

### Turn variable pain into a nominal variable
backPain$pain <- factor(backPain$pain, ordered = FALSE)

### Compare output from sm.ps and VGAM::sm.ps (it is the same)
head(sm.ps(backPain$x1, ps.int = 3, degree = 2))
head(VGAM::sm.ps(backPain$x1, ps.int = 3, degree = 2))

sum(sm.ps(backPain$x1, ps.int = 3, degree = 2) - VGAM::sm.ps(backPain$x1, ps.int = 3, degree = 2))

str(sm.ps(backPain$x1, ps.int = 3, degree = 2))
str(VGAM::sm.ps(backPain$x1, ps.int = 3, degree = 2))

### Fit multinomial logistic regression model with sm.ps and VGAM::sm.ps applied to x1
## No error
calib.model1 <- VGAM::vgam(pain ~ sm.ps(x1, ps.int = 3, degree = 2),
                           data = backPain, family = VGAM::multinomial(refLevel = 1))

## Error
calib.model2 <- VGAM::vgam(pain ~ smps11 + smps12 + smps13 + smps14,
                           data = backPain.merge, family = VGAM::multinomial(refLevel = 1))


### Calculating the variables manually and including in the model leads to the same error, regardless of whether sm.ps or VGAM::sm.ps is used

## Calculate using sm.ps
smps1 <- sm.ps(backPain$x1, ps.int = 3, degree = 2)
colnames(smps1) <- paste("smps1", 1:ncol(smps1), sep = "")
backPain.merge <- cbind(backPain, smps1)

calib.model <- VGAM::vgam(pain ~ smps11 + smps12 + smps13 + smps14, data = backPain.merge, family = VGAM::multinomial(refLevel = 1))

## Calculate using VGAM::sm.ps
smps1 <- VGAM::sm.ps(backPain$x1, ps.int = 3, degree = 2)
colnames(smps1) <- paste("smps1", 1:ncol(smps1), sep = "")
backPain.merge <- cbind(backPain, smps1)

calib.model <- VGAM::vgam(pain ~ smps11 + smps12 + smps13 + smps14, data = backPain.merge, family = VGAM::multinomial(refLevel = 1))

一个解决方案是将

VGAM
列为依赖项,而不是导入,然后我可以调用
sm.ps
而不必调用
VGAM::sm.ps
,但是这个模型只会构成我的包的一小部分,并且因此,我不希望用户必须附加整个
VGAM
包。

r spline vgam
© www.soinside.com 2019 - 2024. All rights reserved.