arima.sim2 <- function(model, n, rand.gen = rnorm,
                      innov = rand.gen(n, ...), n.start = NA, ...)
{
    if(!is.list(model)) stop("'model' must be list")
    p <- length(model$ar)
    if(p) {
        minroots <- min(Mod(polyroot(c(1, -model$ar))))
        if(minroots <= 1) stop("'ar' part of model is not stationary")
    }
    q <- length(model$ma)
    if(is.na(n.start)) n.start <- p + q +
        ifelse(p > 0, ceiling(6/log(minroots)), 0)
    if(n.start < p + q) stop("burn-in 'n.start' must be as long as 'ar + ma'")
    d <- 0
    if(!is.null(ord <- model$order)) {
        if(length(ord) != 3) stop("'model$order' must be of length 3")
        if(p != ord[1]) stop("inconsistent specification of 'ar' order")
        if(q != ord[3]) stop("inconsistent specification of 'ma' order")
        d <- ord[2]
        if(d != round(d) || d < 0)
            stop("number of differences must be a positive integer")
    }
    x <- ts(c(rand.gen(n.start, ...), innov[1:n]), start = 1 - n.start)
    if(length(model$ma)) x <- filter(x, c(1, model$ma), sides = 1)
    if(length(model$ar)) x <- filter(x, model$ar, method = "recursive")
    if(n.start > 0) x <- x[-(1:n.start)]
    if(d > 0) x <- diffinv(x, differences = d)
    as.ts(x)
}