1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
##' @export
updatelvm <- function(x,mean=TRUE,...) {
index(x) <- reindex(x,mean=mean,...)
x$parpos <- parpos(x,mean=mean,...)
return(x)
}
##' @export
"index" <- function(x,...) UseMethod("index")
##' @export
"index<-" <- function(x,...,value) UseMethod("index<-")
##' @export
"index.lvm" <- function(x,...) { x$index }
##' @export
"index.lvmfit" <- function(x,...) { index(Model(x)) }
##' @export
"index<-.lvm" <- function(x,...,value) { x$index <- value; return(x) }
##' @export
"index<-.lvmfit" <- function(x,...,value) { Model(x)$index <- value; return(x) }
### A ## Matrix with fixed parameters and ones where parameters are free
### J ## Manifest variable selection matrix
### M0 ## Index of free regression parameters
### M1 ## Index of free and _unique_ regression parameters
### P ## Matrix with fixed variance parameters and ones where parameters are free
### P0 ## Index of free variance parameters
### P1 ## Index of free and _unique_ regression parameters
### npar.var ## Number of covariance parameters
##' @export
`reindex` <-
function(x, sparse=FALSE,standard=TRUE,zeroones=FALSE,deriv=FALSE,mean=TRUE) { ## Extract indices of parameters from model
x$parpos <- NULL
M <- x$M
eta <- latent(x) ## Latent variables/Factors
m <- length(eta)
obs <- manifest(x) ## Manifest/Observed variables
endo <- endogenous(x)
exo <- exogenous(x) ##,index=FALSE)
allvars <- vars(x)
eta.idx <- na.omit(match(eta,allvars))
obs.idx <- na.omit(match(obs,allvars))
exo.idx <- na.omit(match(exo,allvars))
exo.obsidx <- na.omit(match(exo,obs))
endo.obsidx <- na.omit(match(endo,obs))
fix.idx <- !is.na(x$fix) ## Index of fixed parameters
covfix.idx <- !is.na(x$covfix) ## Index of fixed covariance parameters
constrain.par <- NULL
if (length(constrain(x))>0) constrain.par <- names(constrain(x))
M0 <- M; M0[fix.idx] <- 0 ## Matrix of indicators of free regression-parameters (removing fixed parameters)
M1 <- M0; ## Matrix of indiciator of free _unique_ regression parameters (removing fixed _and_ duplicate parameters)
parname <- unique(x$par[!is.na(x$par)])
for (p in parname) {
ii <- which(x$par==p)
if (length(ii)>1)
M1[ii[-1]] <- 0
if (p %in% constrain.par)
M0[ii] <- M1[ii] <- 0
}
npar.reg <- sum(M1) ## Number of free regression parameters
P <- x$cov;
P0 <- P; P0[covfix.idx] <- 0 ## Matrix of indicators of free covariance-parameters (removing fixed parameters)
if (length(exo.idx)>0)
P0[exo.idx,exo.idx] <- 0 ## 6/1-2011
P1 <- P0 ## Matrix of indiciator of free _unique_ variance parameters (removing fixed _and_ duplicate parameters)
covparname <- unique(x$covpar[!is.na(x$covpar)])
for (p in covparname) {
ii <- which(x$covpar==p)
if (length(ii)>1)
P1[ii[-1]] <- 0
if (p%in%c(parname,constrain.par))
P0[ii] <- P1[ii] <- 0
}
npar.var <- sum(c(diag(P1),P1[lower.tri(P1)]))
A <- M
A[fix.idx] <- x$fix[fix.idx] ## ... with fixed parameters in plac
P[covfix.idx] <- x$covfix[covfix.idx] ## ... with fixed parameters in plac
px <- Jy <- J <- diag(nrow=length(vars(x)))
if (m>0) {
J[eta.idx,eta.idx] <- 0; J <- J[-eta.idx,,drop=FALSE]
} ## Selection matrix (selecting observed variables)
{
## Selection matrix (selection endogenous variables)
if (length(c(eta.idx,exo.idx))>0) {
Jy[c(eta.idx,exo.idx),c(eta.idx,exo.idx)] <- 0; Jy <- Jy[-c(eta.idx,exo.idx),,drop=FALSE]
}
## Cancelation matrix (cancels rows with exogenous variables)
px[exo.idx,exo.idx] <- 0
}
## Creating indicitor of free mean-parameters
fixed <- sapply(x$mean, function(y) is.numeric(y) & !is.na(y))
named <- sapply(x$mean, function(y) is.character(y) & !is.na(y))
mparname <- NULL
if (length(named)>0)
mparname <- unlist(unique(x$mean[named]))
v0 <- rep(1,length(x$mean)) ## Vector of indicators of free mean-parameters
v0[exo.idx] <- 0
if (length(fixed)>0) v0[fixed] <- 0;
v1 <- v0
for (p in mparname) {
idx <- which(x$mean==p)
if (length(idx)>1) {
v1[idx[-1]] <- 0
}
if (p%in%c(parname,covparname,constrain.par))
v0[idx] <- v1[idx] <- 0
} ## duplicate parameters
###
### Extra parameters
###
efixed <- sapply(x$exfix, function(y) is.numeric(y) & !is.na(y))
enamed <- sapply(x$exfix, function(y) is.character(y) & !is.na(y))
if(length(enamed)>0){
eparname <- unlist(unique(x$exfix[enamed]))
} else{
eparname <- NULL
}
## Extra parameters
e0 <- rep(1,length(x$expar)) ## Indicators of free extra par.
if (length(efixed)>0)
e0[efixed] <- 0
e1 <- e0
for (p in eparname) {
idx <- which(x$exfix==p)
if (length(idx)>1) {
e1[idx[-1]] <- 0
}
if (p%in%c(parname,covparname,constrain.par,mparname))
e0[idx] <- e1[idx] <- 0
} ## duplicate parameters
## Return:
## Adjacency-matrix (M)
## Matrix of regression-parameters (0,1) _with_ fixed parameters (A)
## Matrix of variance-parameters (indicators 0,1) (P)
## Manifest selection matrix (J),
## Position of variables matrix (Apos),
## Position of covariance variables matrix (Ppos),
## Position/Indicator matrix of free regression parameters (M0)
res <- list(vars=allvars, manifest=obs, exogenous=exo, latent=eta,
endogenous=endo,
exo.idx=exo.idx, eta.idx=eta.idx,
exo.obsidx=exo.obsidx, endo.obsidx=endo.obsidx,
obs.idx=obs.idx,
endo.idx=setdiff(obs.idx,exo.idx))
if (standard) {
res <- c(res, list(M=M, A=A, P=P,
P0=P0, P1=P1,
M0=M0, M1=M1,
v0=v0, v1=v1,
e0=e0, e1=e1,
npar=(npar.reg+npar.var),
npar.reg=npar.reg,
npar.var=npar.var,
npar.mean=sum(v1),
npar.ex=sum(e1),
constrain.par=constrain.par))
which.diag <- NULL
if (length(P1)>0)
which.diag <- which(diag(P1==1))
res <- c(res, list(parname.all=parname, parname=setdiff(parname,constrain.par),
which.diag=which.diag,
covparname.all=covparname,
covparname=setdiff(covparname,constrain.par),
meanfixed=fixed, meannamed=named,
mparname.all=mparname,
mparname=setdiff(mparname,constrain.par),
eparname.all=eparname,
eparname=setdiff(eparname,constrain.par),
J=J, Jy=Jy, px=px, sparse=sparse))
parname.all.reg.idx <- parname.all.reg.tidx <-
parname.reg.tidx <- parname.reg.idx <- c()
for (p in res$parname.all) {
ipos <- which((x$par==p))
tipos <- which(t(x$par==p))
if (p%in%res$parname) {
parname.reg.idx <- c(parname.reg.idx, list(ipos))
parname.reg.tidx <- c(parname.reg.tidx, list(tipos))
}
parname.all.reg.idx <- c(parname.all.reg.idx, list(ipos))
parname.all.reg.tidx <- c(parname.all.reg.tidx, list(tipos))
};
if (length(parname.reg.idx)>0) {
names(parname.reg.idx) <- names(parname.reg.tidx) <- res$parname
}
if (length(parname.all.reg.idx)>0) {
names(parname.all.reg.idx) <- names(parname.all.reg.tidx) <- res$parname.all
}
covparname.all.idx <- covparname.idx <- c()
for (p in res$covparname.all) {
ipos <- which(x$covpar==p)
if (p%in%res$covparname)
covparname.idx <- c(covparname.idx, list(ipos))
covparname.all.idx <- c(covparname.all.idx, list(ipos))
};
if (length(covparname.idx)>0)
names(covparname.idx) <- res$covparname
if (length(covparname.all.idx)>0)
names(covparname.all.idx) <- res$covparname.all
mparname.all.idx <- mparname.idx <- c()
for (p in res$mparname.all) {
ipos <- which(x$mean==p)
if (p%in%mparname)
mparname.idx <- c(mparname.idx, list(ipos))
mparname.all.idx <- c(mparname.all.idx, list(ipos))
};
if (length(mparname.idx)>0)
names(mparname.idx) <- res$mparname
if (length(mparname.all.idx)>0)
names(mparname.all.idx) <- res$mparname.all
eparname.all.idx <- eparname.idx <- c()
for (p in res$eparname.all) {
ipos <- which(x$exfix==p)
if (p%in%eparname)
eparname.idx <- c(eparname.idx, list(ipos))
eparname.all.idx <- c(eparname.all.idx, list(ipos))
};
if (length(eparname.idx)>0)
names(eparname.idx) <- res$eparname
if (length(eparname.all.idx)>0)
names(eparname.all.idx) <- res$eparname.all
res <- c(res, list(mparname.idx=mparname.idx,
covparname.idx=covparname.idx,
parname.reg.idx=parname.reg.idx,
parname.reg.tidx=parname.reg.tidx,
mparname.all.idx=mparname.all.idx,
eparname.all.idx=eparname.all.idx,
covparname.all.idx=covparname.all.idx,
parname.all.reg.idx=parname.all.reg.idx,
parname.all.reg.tidx=parname.all.reg.tidx
))
} else {
res <- index(x)
}
if (zeroones) {
if (sparse) {
if (!requireNamespace("Matrix",quietly=TRUE)) stop("package Matrix not available")
Ik <- Matrix::Diagonal(length(obs))
Im <- Matrix::Diagonal(ncol(A))
Kkk <- NULL
J <- as(J, "sparseMatrix")
Jy <- as(Jy, "sparseMatrix")
px <- as(px, "sparseMatrix")
} else {
Ik <- diag(nrow=length(obs))
Im <- diag(nrow=ncol(A))
}
Kkk <- NULL
res[c("Ik","Im","Kkk")] <- NULL
res <- c(res, list(Ik=Ik, Im=Im, Kkk=Kkk))
}
if (deriv && length(P)>0) {
if (res$npar.mean>0 & mean)
D <- deriv.lvm(x,meanpar=rep(1,res$npar.mean),zeroones=TRUE)
else
D <- deriv.lvm(x,meanpar=NULL,zeroones=TRUE)
res[c("dA","dP","dv")] <- NULL
res <- c(res, list(dA=D$dA, dP=D$dP, dv=D$dv))
}
if (length(P)>0)
res <- c(res,mat.lvm(x,res))
return(res)
}
|