I'm attempting to use the nlme
package to fit the Generalized beta of the 2nd kind distribution to simulated health cost data.
Running the following code on a test dataset:
Package installation (if necessary)
install.packages("withr", dependencies = T)
library(withr)
with_makevars(c(PKG_CFLAGS ="-std=gnu99"),
install.packages("cubature"), assignment="+=")
install.packages("GB2", dependencies = T)
install.packages("nlme", dependencies = T)
# load packages
library(cubature)
library(GB2)
library(nlme)
# Binary independent variables
age <- rbinom(n=1000, size=1, prob=.3)
sex <- rbinom(n=1000, size=1, prob=.5)
trmt <- rbinom(n=1000, size=1, prob=.5)
# GB2 parameter equations
shape1 <- exp(rnorm(n=1000, mean=.1 + age/100 - sex/10 + trmt/10, sd=.3))
scale <- exp(rnorm(n=1000, mean=7 + age/50 + sex - trmt, sd=.5))
shape2 <- exp(rnorm(n=1000, mean=1.5 + age/100 + sex/10 - trmt/10, sd=.3))
shape3 <- exp(rnorm(n=1000, mean=.5 + age/100 - sex/10 - trmt/10, sd=.3))
# Outcome
y <- rgb2(1000, shape1, scale, shape2, shape3)
# Create test dataset
df <- data.frame(cbind(y,age,sex,trmt,shape1,scale,shape2,shape3))
# Fit GB2 distribution to data
gb2_fit <- nlme(y ~ scale*beta(shape2 + 1/shape1, shape3 - 1/shape1)/beta(shape2, shape3),
# data = list(y=df_gb2_test[,1]),
data = df,
fixed = list(shape1 ~ age + sex + trmt,
scale ~ age + sex + trmt,
shape2 ~ age + sex + trmt,
shape3 ~ age + sex + trmt),
start = list(fixed = c(shape1 = 1.00, scale = 100, shape2 = 1.00, shape3 = 1.00)))
I get the error:
Error in parse(text = paste("~", paste(nVal, collapse = "/"))) :
<text>:2:0: unexpected end of input
1: ~
^
Any ideas what I'm doing wrong? I seem to be using the tilde operator correctly.