tests/testthat/test-initial.R

set.seed(242)
n<-250
x1<-sample(c(0,1),n,replace=TRUE,prob=c(0.75,0.25))
x2<-vector("numeric",n)
x2[x1==0]<-sample(c(0,1),n-sum(x1==1),replace=TRUE,prob=c(2/3,1/3))
z<-rnorm(n,0.5)
# create latent outcome variable
latenty<-0.5+1.5*x1-0.5*x2+0.5*z+rnorm(n,sd=exp(0.5*x1-0.5*x2))
# observed y has four possible values: -1,0,1,2
# threshold values are: -0.5, 0.5, 1.5.
y<-vector("numeric",n)
y[latenty< -0.5]<--1
y[latenty>= -0.5 & latenty<0.5]<- 0
y[latenty>= 0.5 & latenty<1.5]<- 1
y[latenty>= 1.5]<- 2
dataset<-data.frame(y,x1,x2)

testthat::test_that("Default initial value is 'default'", {

  mod1 <- oglm::oglmx(y ~ x1 + x2 + z,
                     data=dataset,link="probit",constantMEAN=FALSE,
                     constantSD=FALSE,delta=0,threshparam=NULL, start_method = "default")
  mod2 <- oglm::oglmx(y ~ x1 + x2 + z,
                     data=dataset,link="probit",constantMEAN=FALSE,
                     constantSD=FALSE,delta=0,threshparam=NULL)
  mod1$call <- NULL
  mod2$call <- NULL

  testthat::expect_identical(
    mod1,
    mod2
  )
})

testthat::test_that("Changing initial value to 'search' does not change result for well conditioned output", {

  mod1 <- oglm::oglmx(y ~ x1 + x2 + z,
                      data=dataset,link="probit",constantMEAN=FALSE,
                      constantSD=FALSE,delta=0,threshparam=NULL, start_method = "search")
  mod2 <- oglm::oglmx(y ~ x1 + x2 + z,
                      data=dataset,link="probit",constantMEAN=FALSE,
                      constantSD=FALSE,delta=0,threshparam=NULL)

  testthat::expect_equal(
    mod1$coefficients,
    mod2$coefficients
  )
})
linogaliana/oglm documentation built on March 5, 2021, 8:27 p.m.