context("Inference") test_that("Effects",{ m <- lvm() regression(m) <- c(y1,y2,y3)~u; latent(m) <- ~u regression(m) <- c(z1,z2,z3)~v; latent(m) <- ~v regression(m) <- u~v regression(m) <- c(u,v,z3,y1)~x d <- sim(m,100,seed=1) start <- c(rep(0,6),rep(1,17)) suppressWarnings(e <- estimate(m,d,control=list(iter.max=0,start=start))) f <- summary(ef <- effects(e,y1~x))$coef testthat::expect_true(all(f[,2]>0)) ## Std.err testthat::expect_equal(f["Total",1],3) testthat::expect_equal(f["Direct",1],1) f2 <- summary(effects(e,u~v))$coef testthat::expect_equal(f2["Total",1],1) testthat::expect_equal(f2["Direct",1],1) testthat::expect_equal(f2["Indirect",1],0) testthat::expect_output(print(ef),"Mediation proportion") testthat::expect_equivalent(confint(ef)["Direct",], confint(e)["y1~x",]) testthat::expect_equivalent(totaleffects(e,y1~x)[,1:4],f["Total",]) ##g <- graph::updateGraph(plot(m,noplot=TRUE)) ##testthat::expect_equivalent(path(g,y1~x),path(m,y1~x)) }) test_that("Profile confidence limits", { m <- lvm(y~b*x) constrain(m,b~psi) <- identity set.seed(1) d <- sim(m,100,seed=1) e <- estimate(m, d) ci0 <- confint(e,3) ci <- confint(e,3,profile=TRUE) testthat::expect_true(mean((ci0-ci)^2)<0.1) }) test_that("IV-estimator", { m <- lvm(c(y1,y2,y3)~u); latent(m) <- ~u set.seed(1) d <- sim(m,100,seed=1) e0 <- estimate(m,d) e <- estimate(m,d,estimator="iv") ## := MLE testthat::expect_true(mean((coef(e)-coef(e0))^2)<1e-9) }) test_that("glm-estimator", { m <- lvm(y~x+z) regression(m) <- x~z distribution(m,~y+z) <- binomial.lvm("logit") set.seed(1) d <- sim(m,1e3,seed=1) head(d) e <- estimate(m,d,estimator="glm") c1 <- coef(e,2)[c("y","y~x","y~z"),1:2] c2 <- estimate(glm(y~x+z,d,family=binomial))$coefmat[,1:2] testthat::expect_equivalent(c1,c2) }) test_that("gaussian", { m <- lvm(y~x) d <- simulate(m,100,seed=1) S <- cov(d[,vars(m),drop=FALSE]) mu <- colMeans(d[,vars(m),drop=FALSE]) f <- function(p) lava:::gaussian_objective.lvm(p,x=m,S=S,mu=mu,n=nrow(d)) g <- function(p) lava:::gaussian_score.lvm(p,x=m,n=nrow(d),data=d,indiv=TRUE) s1 <- numDeriv::grad(f,c(0,1,1)) s2 <- g(c(0,1,1)) testthat::expect_equal(s1,-colSums(s2),tolerance=0.1) }) if (requireNamespace("mets",quietly = TRUE)) test_that("Association measures", { P <- matrix(c(0.25,0.25,0.25,0.25),2) a1 <- lava:::assoc(P) testthat::expect_equivalent(-log(0.25),a1$H) testthat::expect_true(with(a1, all(c(kappa,gamma,MI,U.sym)==0))) p <- lava:::prob.normal(sigma=diag(nrow=2),breaks=c(-Inf,0),breaks2=c(-Inf,0))[1] testthat::expect_equal(p[1],0.25) ## q <- qnorm(0.75) ## m <- ordinal(lvm(y~x),~y, K=3)#, breaks=c(-q,q)) ## normal.threshold(m,p=c(0,1,2)) }) test_that("equivalence", { m <- lvm(c(y1,y2,y3)~u,u~x,y1~x) latent(m) <- ~u d <- sim(m,100,seed=12) cancel(m) <- y1~x regression(m) <- y2~x e <- estimate(m,d) ##eq <- equivalence(e,y1~x,k=1) dm <- capture.output(eq <- equivalence(e,y2~x,k=1)) testthat::expect_output(print(eq),paste0("y1",lava.options()$symbol[2],"y3")) testthat::expect_true(all(c("y1","y3")%in%eq$equiv[[1]][1,])) }) test_that("multiple testing", { testthat::expect_equivalent(lava:::holm(c(0.05/3,0.025,0.05)),rep(0.05,3)) ci1 <- scheffe(l <- lm(1:5~c(0.5,0.7,1,1.3,1.5))) ci2 <- predict(l,interval="confidence") testthat::expect_equivalent(ci1[,1],ci2[,1]) testthat::expect_true(all(ci1[,2]ci2[,3])) }) test_that("modelsearch and GoF", { m <- lvm(c(y1,y2)~x) d <- sim(m,100,seed=1) e <- estimate(lvm(c(y1,y2)~1,y1~x),d) e0 <- estimate(lvm(c(y1,y2)~x,y1~~y2),d) s1 <- modelsearch(e,silent=TRUE,type="correlation") testthat::expect_true(nrow(s1$res)==2) s1b <- modelsearch(e,silent=TRUE,type="regression") testthat::expect_true(nrow(s1b$res)==4) s2 <- modelsearch(e0,silent=TRUE,dir="backward") testthat::expect_true(nrow(s2$res)==3) e00 <- estimate(e0,vcov=vcov(e0))$coefmat ii <- match(s2$res[,"Index"],rownames(e00)) testthat::expect_equivalent(e00[ii,5],s2$test[,2]) s3 <- modelsearch(e0,dir="backward",k=3) testthat::expect_true(nrow(s3$res)==1) ee <- modelsearch(e0,dir="backstep",messages=FALSE) testthat::expect_true(inherits(ee,"lvm")) ## TODO gof(e,all=TRUE) r <- rsq(e)[[1]] testthat::expect_true(abs(summary(lm(y1~x,d))$r.square-r["y1"])<1e-5) }) test_that("Bootstrap", { y <- rep(c(0,1),each=5) x <- 1:10 e <- estimate(y~x,lvm=TRUE) B1 <- bootstrap(e,R=2,silent=TRUE,mc.cores=1,sd=TRUE) B2 <- bootstrap(e,R=2,silent=TRUE,bollenstine=TRUE,mc.cores=1) testthat::expect_false(B1$bollenstine) testthat::expect_true(B2$bollenstine) testthat::expect_true(nrow(B1$coef)==2) testthat::expect_output(print(B1),"Standard errors:") # if (requireNamespace("future",quietly=TRUE)) future::plan("sequential") dm <- capture.output(B3 <- bootstrap(e,R=2,fun=function(x) coef(x)[2]^2+10)) testthat::expect_true(all(mean(B3$coef)>10)) y <- rep(c(0,1),each=5) x <- 1:10 m <- lvm(y~b*x) constrain(m,alpha~b) <- function(x) x^2 e <- estimate(m,data.frame(y=y,x=x)) b <- bootstrap(e,R=1,silent=TRUE) testthat::expect_output(print(b),"alpha") }) test_that("Survreg", { m <- lvm(y0~x) transform(m,y~y0) <- function(x) pmin(x[,1],2) transform(m,status~y0) <- function(x) x<2 d <- simulate(m,100,seed=1) require('survival') m <- survreg(Surv(y,status)~x,data=d,dist='gaussian') s <- score(m) testthat::expect_true(length(pars(m))==length(coef(m))+1) testthat::expect_true(abs(attr(score(m,pars(m)),'logLik')-logLik(m))<1e-9) testthat::expect_true(mean(colSums(s)^2)<1e-6) testthat::expect_equivalent(vcov(m), attr(s,'bread')/nrow(d)) }) test_that("Combine", { ## Combine model output data(serotonin) m1 <- lm(cau ~ age*gene1 + age*gene2,data=serotonin) m2 <- lm(cau ~ age + gene1,data=serotonin) cc <- Combine(list('model A'=m1,'model B'=m2),fun=function(x) c(R2=format(summary(x)$r.squared,digits=2))) testthat::expect_true(nrow(cc)==length(coef(m1))+1) testthat::expect_equivalent(colnames(cc),c('model A','model B')) testthat::expect_equivalent(cc['R2',2],format(summary(m2)$r.squared,digits=2)) }) test_that("zero-inflated binomial regression (zib)", { set.seed(1) n <- 1e3 x <- runif(n,0,20) age <- runif(n,10,30) z0 <- rnorm(n,mean=-1+0.05*age) z <- cut(z0,breaks=c(-Inf,-1,0,1,Inf)) p0 <- lava::expit(model.matrix(~z+age) %*% c(-.4, -.4, 0.2, 2, -0.05)) y <- (runif(n)0 df <- function(x) 2*x*log(x) + x df2 <- function(x) 2*log(x) + 3 op <- NR(5,f,df,df2,control=list(tol=1e-40)) ## Find root testthat::expect_equivalent(round(op$par,digits=7),.6065307) op2 <- estimatingfunction0(5,gradient=df) op3 <- estimatingfunction(5,gradient=df,hessian=df2,control=list(tol=1e-40)) testthat::expect_equivalent(op$par,op2$par) testthat::expect_equivalent(op$par,op3$par) }) if (requireNamespace("nlme",quietly = TRUE) && requireNamespace("mets",quietly = TRUE)) test_that("Prediction with missing data, random intercept", { ## Random intercept model m <- lvm(c(y1,y2,y3)~u[0]) latent(m) <- ~u regression(m) <- y1~x1 regression(m) <- y2~x2 regression(m) <- y3~x3 d <- simulate(m,1e3,seed=1) dd <- reshape(d,varying=list(c('y1','y2','y3'),c('x1','x2','x3')),direction='long',v.names=c('y','x')) ##system.time(l <- lme4::lmer(y~x+(1|id), data=dd, REML=FALSE)) system.time(l <- nlme::lme(y~x,random=~1|id, data=dd, method="ML")) m0 <- lvm(c(y1[m:v],y2[m:v],y3[m:v])~1*u[0]) latent(m0) <- ~u regression(m0,y=c('y1','y2','y3'),x=c('x1','x2','x3')) <- rep('b',3) system.time(e <- estimate(m0,d)) mytol <- 1e-6 mse <- function(x,y=0) mean(na.omit(as.matrix(x)-as.matrix(y))^2) testthat::expect_true(mse(logLik(e),logLik(l))