% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/sim.lvm.R
\name{sim.lvm}
\alias{sim.lvm}
\alias{sim.lvmfit}
\alias{simulate.lvmfit}
\alias{simulate.lvm}
\alias{transform<-}
\alias{transform<-.lvm}
\alias{transform.lvm}
\alias{functional}
\alias{functional<-}
\alias{functional.lvm}
\alias{functional<-.lvm}
\alias{distribution}
\alias{distribution<-}
\alias{distribution.lvm}
\alias{distribution<-.lvm}
\alias{heavytail}
\alias{heavytail<-}
\alias{categorical}
\alias{categorical<-}
\alias{dist_weibull}
\alias{weibull.lvm}
\alias{dist_bernoulli}
\alias{binomial.lvm}
\alias{probit.lvm}
\alias{logit.lvm}
\alias{dist_poisson}
\alias{poisson.lvm}
\alias{dist_uniform}
\alias{uniform.lvm}
\alias{dist_multinomial}
\alias{multinomial.lvm}
\alias{dist_beta}
\alias{beta.lvm}
\alias{dist_gaussian}
\alias{normal.lvm}
\alias{gaussian.lvm}
\alias{dist_mvn}
\alias{mvn.lvm}
\alias{dist_lognormal}
\alias{lognormal.lvm}
\alias{dist_gaussian_mixture2}
\alias{GM2.lvm}
\alias{dist_gaussian_mixture3}
\alias{GM3.lvm}
\alias{dist_pareto}
\alias{pareto.lvm}
\alias{dist_t}
\alias{student.lvm}
\alias{dist_chisq}
\alias{chisq.lvm}
\alias{dist_cox_gompertz}
\alias{coxGompertz.lvm}
\alias{dist_cox_weibull}
\alias{coxWeibull.lvm}
\alias{dist_cox_exponential}
\alias{coxExponential.lvm}
\alias{dist_aalen_exponential}
\alias{aalenExponential.lvm}
\alias{dist_gamma}
\alias{Gamma.lvm}
\alias{gamma.lvm}
\alias{dist_loggamma}
\alias{loggamma.lvm}
\alias{dist_threshold}
\alias{threshold.lvm}
\alias{dist_ones}
\alias{ones.lvm}
\alias{binary.lvm}
\alias{dist_seq}
\alias{Sequence.lvm}
\alias{dist_seqint}
\alias{id.lvm}
\alias{dist_none}
\alias{none.lvm}
\alias{dist_const}
\alias{constant.lvm}
\title{Simulate model}
\usage{
\method{sim}{lvm}(x, n = NULL, p = NULL, normal = FALSE, cond = FALSE,
sigma = 1, rho = 0.5, X = NULL, unlink=FALSE, latent=TRUE,
use.labels = TRUE, seed=NULL, ...)
}
\arguments{
\item{x}{Model object}

\item{n}{Number of simulated values/individuals}

\item{p}{Parameter value (optional)}

\item{normal}{Logical indicating whether to simulate data from a
multivariate normal distribution conditional on exogenous variables hence
ignoring functional/distribution definition}

\item{cond}{for internal use}

\item{sigma}{Default residual variance (1)}

\item{rho}{Default covariance parameter (0.5)}

\item{X}{Optional matrix of fixed values of variables (manipulation)}

\item{unlink}{Return Inverse link transformed data}

\item{latent}{Include latent variables (default TRUE)}

\item{use.labels}{convert categorical variables to factors before applying transformation}

\item{seed}{Random seed}

\item{\dots}{Additional arguments to be passed to the low level functions}
}
\description{
Simulate data from a general SEM model including non-linear effects and
general link and distribution of variables.
}
\examples{
##################################################
## Logistic regression
##################################################
m <- lvm(y~x+z)
regression(m) <- x~z
distribution(m,~y+z) <- dist_bernoulli("logit")
d <- sim(m,1e3)
head(d)

e <- estimate(m,d,estimator="glm")
e
## Simulate a few observation from estimated model
sim(e,n=5)

##################################################
## Poisson
##################################################
distribution(m,~y) <- dist_poisson()
d <- sim(m,1e4,p=c(y=-1,"y~x"=2,z=1))
head(d)
estimate(m,d,estimator="glm")
mean(d$z); lava:::expit(1)

summary(lm(y~x,sim(lvm(y[1:2]~4*x),1e3)))

##################################################
### Gamma distribution
##################################################
m <- lvm(y~x)
distribution(m,~y+x) <- list(dist_gamma(shape=2), dist_bernoulli())
intercept(m,~y) <- 0.5
d <- sim(m,1e4)
summary(g <- glm(y~x,family=Gamma(),data=d))
\dontrun{MASS::gamma.shape(g)}

args(lava::dist_gamma)
distribution(m,~y) <- dist_gamma(shape=2,log=TRUE)
sim(m,10,p=c(y=0.5))[,"y"]

##################################################
### Beta
##################################################
m <- lvm()
distribution(m,~y) <- dist_beta(alpha=2,beta=1)
var(sim(m,100,"y,y"=2))
distribution(m,~y) <- dist_beta(alpha=2,beta=1,scale=FALSE)
var(sim(m,100))

##################################################
### Transform
##################################################
m <- lvm()
transform(m,xz~x+z) <- function(x) x[1]*(x[2]>0)
regression(m) <- y~x+z+xz
d <- sim(m,1e3)
summary(lm(y~x+z + x*I(z>0),d))

##################################################
### Non-random variables
##################################################
m <- lvm()
distribution(m,~x+z+v+w) <- list(
  dist_seqint(0,5),## Seq. 0 to 5 by 1/n
  dist_ones(),       ## Vector of ones
  dist_ones(0.5),    ##  0.5n 0, 0.5n 1
  dist_ones(interval=list(c(0.3,0.5),c(0.8,1)))
)
sim(m,10)

##################################################
### Cox model
### piecewise constant hazard
################################################
m <- lvm(t~x)
rates <- c(1,0.5); cuts <- c(0,5)
## Constant rate: 1 in [0,5), 0.5 in [5,Inf)
distribution(m,~t) <- dist_cox_exponential(rate=rates,timecut=cuts)

\dontrun{
    d <- sim(m,2e4,p=c("t~x"=0.1)); d$status <- TRUE
    plot(timereg::aalen(survival::Surv(t,status)~x,data=d,
                        resample.iid=0,robust=0),spec=1)
    L <- approxfun(c(cuts,max(d$t)),f=1,
                   cumsum(c(0,rates*diff(c(cuts,max(d$t))))),
                   method="linear")
    curve(L,0,100,add=TRUE,col="blue")
}

##################################################
### Cox model
### piecewise constant hazard, gamma frailty
##################################################
m <- lvm(y~x+z)
rates <- c(0.3,0.5); cuts <- c(0,5)
distribution(m,~y+z) <- list(dist_cox_exponential(rate=rates,timecut=cuts),
                             dist_loggamma(rate=1,shape=1))
\dontrun{
    d <- sim(m,2e4,p=c("y~x"=0,"y~z"=0)); d$status <- TRUE
    plot(timereg::aalen(survival::Surv(y,status)~x,data=d,
                        resample.iid=0,robust=0),spec=1)
    L <- approxfun(c(cuts,max(d$y)),f=1,
                   cumsum(c(0,rates*diff(c(cuts,max(d$y))))),
                   method="linear")
    curve(L,0,100,add=TRUE,col="blue")
}
## Equivalent via transform (here with Aalens additive hazard model)
m <- lvm(y~x)
distribution(m,~y) <- dist_aalen_exponential(rate=rates, timecut=cuts)
distribution(m,~z) <- dist_gamma(rate=1, shape=1)
transform(m,t~y+z) <- prod
sim(m,10)
## Shared frailty
m <- lvm(c(t1,t2)~x+z)
rates <- c(1,0.5); cuts <- c(0,5)
distribution(m,~y) <- dist_aalen_exponential(rate=rates, timecut=cuts)
distribution(m,~z) <- dist_loggamma(rate=1,shape=1)
\dontrun{
mets::fast.reshape(sim(m,100),varying="t")
}

##################################################
### General multivariate distributions
##################################################
\dontrun{
m <- lvm()
distribution(m,~y1+y2,oratio=4) <- VGAM::rbiplackcop
ksmooth2(sim(m,1e4),rgl=FALSE,theta=-20,phi=25)

m <- lvm()
distribution(m,~z1+z2,"or1") <- VGAM::rbiplackcop
distribution(m,~y1+y2,"or2") <- VGAM::rbiplackcop
sim(m,10,p=c(or1=0.1,or2=4))
}

m <- lvm()
distribution(m,~y1+y2+y3,TRUE) <- function(n,...) rmvn0(n,sigma=diag(3)+1)
var(sim(m,100))

## Syntax also useful for univariate generators, e.g.
m <- lvm(y~x+z)
distribution(m,~y,TRUE) <- function(n) rnorm(n,mean=1000)
sim(m,5)
distribution(m,~y,"m1",0) <- rnorm
sim(m,5)
sim(m,5,p=c(m1=100))

##################################################
### Regression design in other parameters
##################################################
## Variance heterogeneity
m <- lvm(y~x)
distribution(m,~y) <- function(n,mean,x) rnorm(n,mean,exp(x)^.5)
if (interactive()) plot(y~x,sim(m,1e3))
## Alternaively, calculate the standard error directly
addvar(m) <- ~sd ## If 'sd' should be part of the resulting data.frame
constrain(m,sd~x) <- function(x) exp(x)^.5
distribution(m,~y) <- function(n,mean,sd) rnorm(n,mean,sd)
if (interactive()) plot(y~x,sim(m,1e3))

## Regression on variance parameter
m <- lvm()
regression(m) <- y~x
regression(m) <- v~x
##distribution(m,~v) <- 0 # No stochastic term
## Alternative:
## regression(m) <- v[NA:0]~x
distribution(m,~y) <- function(n,mean,v) rnorm(n,mean,exp(v)^.5)
if (interactive()) plot(y~x,sim(m,1e3))

## Regression on shape parameter in Weibull model
m <- lvm()
regression(m) <- y ~ z+v
regression(m) <- s ~ exp(0.6*x-0.5*z)
distribution(m,~x+z) <- dist_bernoulli()
distribution(m,~cens) <- dist_cox_weibull(scale=1)
distribution(m,~y) <- dist_cox_weibull(scale=0.1,shape=~s)
eventTime(m) <- time ~ min(y=1,cens=0)

if (interactive()) {
    d <- sim(m,1e3)
    require(survival)
    (cc <- coxph(Surv(time,status)~v+strata(x,z),data=d))
    plot(survfit(cc) ,col=1:4,mark.time=FALSE)
}

##################################################
### Categorical predictor
##################################################
m <- lvm()
## categorical(m,K=3) <- "v"
categorical(m,labels=c("A","B","C")) <- "v"

regression(m,additive=FALSE) <- y~v
\dontrun{
plot(y~v,sim(m,1000,p=c("y~v:2"=3)))
}

m <- lvm()
categorical(m,labels=c("A","B","C"),p=c(0.5,0.3)) <- "v"
regression(m,additive=FALSE,beta=c(0,2,-1)) <- y~v
## equivalent to:
## regression(m,y~v,additive=FALSE) <- c(0,2,-1)
regression(m,additive=FALSE,beta=c(0,4,-1)) <- z~v
table(sim(m,1e4)$v)
glm(y~v, data=sim(m,1e4))
glm(y~v, data=sim(m,1e4,p=c("y~v:1"=3)))

transform(m,v2~v) <- function(x) x=='A'
sim(m,10)

##################################################
### Pre-calculate object
##################################################
m <- lvm(y~x)
m2 <- sim(m,'y~x'=2)
sim(m,10,'y~x'=2)
sim(m2,10) ## Faster
}
\author{
Klaus K. Holst
}
\keyword{datagen}
\keyword{models}
\keyword{regression}
