0

I am trying to fit a multinomial logistic regression model with a vector spline smoother on the predictors. I am using the VGAM package in R (specifically the vgam function to fit the model). I have used this numerous times before and always just called "sm.ps" in the formula to apply the vector spline smoother.

However, I am trying to write my own package which has VGAM as an import. This means I need to call on functions from the VGAM package using VGAM:: notation. Upon doing this, I am now getting incorrect results when I fit my model. I have identified the issue as that when I call VGAM::sm.ps(x1) in the function formula, I get different results to when I call sm.ps(x1).

To get the exact issue I am encountering would require sharing a lot of code, so I have created a smaller reproducible example (below). Unfortunately this has led to a different error. In my actual problem, the model will fit, but returns erroneous results. In the reproducible example below, the model will not fit, giving the following error:

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

I hope diagnosing what is wrong with my reproducible example, will still lead to a solution in my actual problem.

### Load VGAM and backpain dataset
library(VGAM)
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 ~ VGAM::sm.ps(x1, ps.int = 3, degree = 2), data = backPain, 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))

One solution would be to list VGAM as a dependency, rather than an import, then I could call on sm.ps without having to call VGAM::sm.ps, however this model will only make up a minor part of my package, and I therefore do not want users to have to attach the entire VGAM package.

AP30
  • 505
  • 7
  • 18
  • Did you try to `@importFrom VGAM vgam` as opposed to `@import VGAM`? See more [here](https://r-pkgs.org/dependencies-in-practice.html#sec-dependencies-NAMESPACE-workflow) for example in the [R Packages (2e)](https://r-pkgs.org) book. – dipetkov Mar 14 '23 at 23:11

0 Answers0