[Depmix-commits] r676 - in pkg/depmixS4: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Nov 14 17:15:18 CET 2018


Author: ingmarvisser
Date: 2018-11-14 17:15:17 +0100 (Wed, 14 Nov 2018)
New Revision: 676

Added:
   pkg/depmixS4/man/formatperc.Rd
   pkg/depmixS4/man/vcov.Rd
Modified:
   pkg/depmixS4/NAMESPACE
   pkg/depmixS4/R/confint.R
   pkg/depmixS4/R/format.perc.R
   pkg/depmixS4/R/hessian.R
   pkg/depmixS4/R/standardError.R
   pkg/depmixS4/R/vcov.R
   pkg/depmixS4/man/depmix.Rd
   pkg/depmixS4/man/depmix.fit.Rd
Log:
=added documentation for vcov and friends

Modified: pkg/depmixS4/NAMESPACE
===================================================================
--- pkg/depmixS4/NAMESPACE	2018-11-14 11:33:25 UTC (rev 675)
+++ pkg/depmixS4/NAMESPACE	2018-11-14 16:15:17 UTC (rev 676)
@@ -3,7 +3,7 @@
 importFrom(stats, predict, simulate)
 
 importFrom("stats", "cov", "cov.wt", "dbinom", "dgamma", "dmultinom",
-             "dnorm", "dpois", "gaussian", "glm.fit", "lm.fit",
+             "dnorm", "qnorm", "dpois", "gaussian", "glm.fit", "lm.fit",
              "lm.wfit", "mahalanobis", "make.link", "model.frame",
              "model.matrix", "model.response", "pchisq", "rbinom",
              "rgamma", "rmultinom", "rnorm", "rpois", "sd")
@@ -23,17 +23,15 @@
 	em.control,
 	viterbi,
 	mlogit,
-	logLik,
-	hessian2vcov,
-	format.perc
+	logLik
 )
 
 exportClasses(
 	depmix,
 	depmix.sim,
+	depmix.fitted,
 	mix,
 	mix.sim,
-	depmix.fitted,
 	mix.fitted,
 	response,
 	GLMresponse,

Modified: pkg/depmixS4/R/confint.R
===================================================================
--- pkg/depmixS4/R/confint.R	2018-11-14 11:33:25 UTC (rev 675)
+++ pkg/depmixS4/R/confint.R	2018-11-14 16:15:17 UTC (rev 676)
@@ -19,9 +19,9 @@
 # 
 
 setMethod("confint", "mix",
-    function(object, level=0.95, digits=4, fixed=NULL, equal=NULL, 
+    function(object, level=0.95, fixed=NULL, equal=NULL, 
 	conrows=NULL, conrows.upper=NULL, conrows.lower=NULL, 
-	tolerance=1e-9, 	
+	tolerance=1e-6, 	
 	method="finiteDifferences", ...) {
 		
 	vc <- vcov(object,fixed=fixed,equal=equal,
@@ -39,13 +39,14 @@
 	upper <- parsinc+qnorm(0.5+level/2)*ses
 	lower <- parsinc-qnorm(0.5+level/2)*ses
 	
-	ret <- data.frame(pars=round(pars,digits), constr=elements, lower=NA, upper=NA)
+	ret <- data.frame(par=pars, constr=elements, lower=NA, upper=NA)
 		
-	ret$lower[which(elements=="inc")] <- round(lower, digits)
-	ret$upper[which(elements=="inc")] <- round(upper, digits)
+	ret$lower[which(elements=="inc")] <- lower
+	ret$upper[which(elements=="inc")] <- upper
 		
-	colnames(ret)[3:4] <- c(format.perc(0.5-level/2, 3),format.perc(0.5+level/2, 3))
+	colnames(ret)[3:4] <- c(formatperc(0.5-level/2, 3),formatperc(0.5+level/2, 3))
 	
 	return(ret)
+	
 }
 )

Modified: pkg/depmixS4/R/format.perc.R
===================================================================
--- pkg/depmixS4/R/format.perc.R	2018-11-14 11:33:25 UTC (rev 675)
+++ pkg/depmixS4/R/format.perc.R	2018-11-14 16:15:17 UTC (rev 676)
@@ -1,5 +1,6 @@
-format.perc <-
-function (probs, digits) {
-    paste(format(100 * probs, trim = TRUE, 
-                 scientific = FALSE, digits = digits), "%")
+
+formatperc <-
+function (x, digits) {
+    paste(format(100 * x, trim = TRUE, 
+                 scientific = FALSE, digits = digits), "%", sep="")
 }

Modified: pkg/depmixS4/R/hessian.R
===================================================================
--- pkg/depmixS4/R/hessian.R	2018-11-14 11:33:25 UTC (rev 675)
+++ pkg/depmixS4/R/hessian.R	2018-11-14 16:15:17 UTC (rev 676)
@@ -29,7 +29,7 @@
 
 setMethod("hessian", "mix",
     function(object, 
-	tolerance=1e-9, 	
+	tolerance=1e-6, 	
 	method="finiteDifferences", ...) {
 
 	if(is.nan(logLik(object))) stop("Log likelihood is 'NaN'; cannot compute hessian. ")
@@ -39,10 +39,9 @@
 	
 	# get parameter boundaries from the model object
 	constraints <- getConstraints(object)
+	par.u <- constraints$par.u
+	par.l <- constraints$par.l
 	
-	par.u=constraints$par.u
-	par.l=constraints$par.l
-	
 	# get fixed parameters
 	fixed <- getpars(object,"fixed")
 	
@@ -66,7 +65,7 @@
 	logl <- function(pars) {
 		allpars[which(elements=="inc")] <- pars
 		object <- setpars(object,allpars)
-		ans = -as.numeric(logLik(object))
+		ans <- -as.numeric(logLik(object))
 		if(is.na(ans)) ans = 1000000 # remove magic number here!!!
 		ans
 	}

Modified: pkg/depmixS4/R/standardError.R
===================================================================
--- pkg/depmixS4/R/standardError.R	2018-11-14 11:33:25 UTC (rev 675)
+++ pkg/depmixS4/R/standardError.R	2018-11-14 16:15:17 UTC (rev 676)
@@ -19,9 +19,9 @@
 #
 
 setMethod("standardError", "mix",
-    function(object, digits=4, fixed=NULL, equal=NULL, 
+    function(object, fixed=NULL, equal=NULL, 
 	conrows=NULL, conrows.upper=NULL, conrows.lower=NULL, 
-	tolerance=1e-9, 	
+	tolerance=1e-6, 	
 	method="finiteDifferences", ...) {
 		
 	vc <- vcov(object,fixed=fixed,equal=equal,
@@ -36,9 +36,9 @@
 	
 	parsinc <- pars[which(elements=="inc")]
 		
-	ret <- data.frame(pars=round(pars,digits), constr=elements, ses=NA)
+	ret <- data.frame(par=pars, constr=elements, se=NA)
 	
-	ret$ses[which(elements=="inc")] <- round(ses,digits)
+	ret$ses[which(elements=="inc")] <- ses
 	
 	return(ret)
 }

Modified: pkg/depmixS4/R/vcov.R
===================================================================
--- pkg/depmixS4/R/vcov.R	2018-11-14 11:33:25 UTC (rev 675)
+++ pkg/depmixS4/R/vcov.R	2018-11-14 16:15:17 UTC (rev 676)
@@ -32,7 +32,7 @@
 setMethod("vcov", "mix",
     function(object, fixed=NULL, equal=NULL, 
 	conrows=NULL, conrows.upper=NULL, conrows.lower=NULL, 
-	tolerance=1e-9, 	
+	tolerance=1e-6, 	
 	method="finiteDifferences", ...) {
 	
 	if(is.nan(logLik(object))) stop("Log likelihood is 'NaN'; cannot compute variance-covariance. ")

Modified: pkg/depmixS4/man/depmix.Rd
===================================================================
--- pkg/depmixS4/man/depmix.Rd	2018-11-14 11:33:25 UTC (rev 675)
+++ pkg/depmixS4/man/depmix.Rd	2018-11-14 16:15:17 UTC (rev 676)
@@ -183,7 +183,7 @@
 
 	Lawrence R. Rabiner (1989).  A tutorial on hidden Markov models and
 	selected applications in speech recognition.  \emph{Proceedings of
-	IEEE}, 77-2, p.  267-295.
+	IEEE}, 77-2, p. 267-295.
 	
 }
 

Modified: pkg/depmixS4/man/depmix.fit.Rd
===================================================================
--- pkg/depmixS4/man/depmix.fit.Rd	2018-11-14 11:33:25 UTC (rev 675)
+++ pkg/depmixS4/man/depmix.fit.Rd	2018-11-14 16:15:17 UTC (rev 676)
@@ -28,7 +28,7 @@
 
 	\S4method{fit}{mix}(object, fixed=NULL, equal=NULL, 
 		conrows=NULL, conrows.upper=NULL, conrows.lower=NULL, 
-		method=NULL, verbose=TRUE,
+		method=NULL, verbose=FALSE,
 		emcontrol=em.control(),
 		solnpcntrl=list(rho = 1, outer.iter = 400, inner.iter = 800, 
 		delta = 1e-7, tol = 1e-8),

Added: pkg/depmixS4/man/formatperc.Rd
===================================================================
--- pkg/depmixS4/man/formatperc.Rd	                        (rev 0)
+++ pkg/depmixS4/man/formatperc.Rd	2018-11-14 16:15:17 UTC (rev 676)
@@ -0,0 +1,14 @@
+\name{formatperc}
+
+\alias{formatperc}
+
+\title{Format percentage for level in printing confidence interval}
+
+\description{See title.}
+
+\usage{formatperc(x,digits)}
+
+\arguments{
+	\item{x}{a vector of probabilities to be formatted as percentages.}
+	\item{digits}{the number of required digits in the percentages.}
+}

Added: pkg/depmixS4/man/vcov.Rd
===================================================================
--- pkg/depmixS4/man/vcov.Rd	                        (rev 0)
+++ pkg/depmixS4/man/vcov.Rd	2018-11-14 16:15:17 UTC (rev 676)
@@ -0,0 +1,160 @@
+\name{vcov}
+
+\alias{vcov}
+\alias{confint}
+\alias{standardError}
+\alias{hessian}
+
+\alias{vcov,mix-method}
+\alias{confint,mix-method}
+\alias{standardError,mix-method}
+\alias{hessian,mix-method}
+
+\title{ Parameter standard errors }
+
+\description{
+
+	These functions provide standard errors for parameters of (dep-)mix models. 
+
+}
+
+\usage{
+	\S4method{vcov}{mix}(object, fixed=NULL, equal=NULL, 
+		conrows=NULL, conrows.upper=NULL, conrows.lower=NULL, tolerance=1e-6, 
+		method="finiteDifferences", ...)	
+	
+	\S4method{standardError}{mix}(object, fixed=NULL, equal=NULL, 
+		conrows=NULL, conrows.upper=NULL, conrows.lower=NULL, tolerance=1e-6, 
+		method="finiteDifferences", ...)	
+	
+	\S4method{confint}{mix}(object, level=0.95, fixed=NULL, equal=NULL, 
+		conrows=NULL, conrows.upper=NULL, conrows.lower=NULL, tolerance=1e-6, 
+		method="finiteDifferences", ...)	
+
+	\S4method{hessian}{mix}(object, tolerance=1e-6, 	
+			method="finiteDifferences", ...)
+}
+
+
+\arguments{
+
+  \item{object}{A (dep-)mix object; see \link{depmix} for details.}
+  
+  \item{fixed, equal}{These arguments are used to specify constraints on a model; see usage details here: \code{\link{fit}}.}
+  
+  \item{conrows}{These arguments are used to specify constraints on a model; see usage details here: \code{\link{fit}}.}
+  
+  \item{conrows.upper}{These arguments are used to specify constraints on a model; see usage details here: \code{\link{fit}}.}
+  
+  \item{conrows.lower}{These arguments are used to specify constraints on a model; see usage details here: \code{\link{fit}}.}
+  
+  \item{tolerance}{Threshold used for testing whether parameters are estimated on the boundary of the parameter space; if so, 
+  they are ignored in these functions.}
+  
+  \item{method}{The method used for computing the Hessian matrix of the parameters; currently only a finite 
+  differences method (using \code{fdHess} from package \code{\link{nlme}}) is implemented and hence used by default.}
+  
+  \item{level}{The desired significance level for the confidence intervals.}
+  
+  \item{...}{Further arguments passed to other methods; currently not in use.}
+  
+}
+
+
+\details{ 
+	
+	\code{vcov} computes the variance-covariance matrix of a (dep-)mix object, either fitted or not. 
+	It does so by first constructing a Hessian matrix through the use of \code{hessian} and then 
+	transforming this as described in Visser et al (2000), taking into account the linear constraints 
+	that are part of the model. Currently, \code{hessian} has a single \code{method} using finite 
+	differences to arrive at an approximation of the second order derivative matrix of the parameters. 
+	
+	\code{confint} and \code{standardError} use \code{vcov} to compute confidence intervals (the confidence
+	level can be set through an argument) and standard errors respectively. The latter are computed first by 
+	using \code{sqrt(diag(vcov))} and the confidence intervals are computed through the normal approximation. 
+	
+	If and when these methods are applied to \code{fit}'ted models, the linear constraint matrix is 
+	obtained from the \code{mix.fitted} or \code{depmix.fitted} slot \code{lincon} (supplemented with 
+	additional constraints if those are provided through the \code{equal} and other arguments to these
+	functions). 
+	
+	All four functions exclude parameters that are estimated on or near (this can be controlled using 
+	the \code{tolerance} argument) their boundary values. Setting this argument to zero can result in
+	error as the \code{fdHess} function requires an environment around the parameter estimate that 
+	provides proper log-likelihood values, which parameter on or over their boundary values are not
+	guaranteed to provided. Fixed parameters are similarly ignored in these four functions. 
+	
+}
+
+\value{
+	
+	\code{vcov} returns a named list with elements \code{vcov}, \code{elements}, and \code{lincon}. 
+	\code{standardError} returns a \code{data.frame} with columns \code{par}, \code{elements}, 
+	and \code{se}. \code{confint} returns a \code{data.frame} with columns \code{par}, 
+	\code{elements}, and two columns for the lower and upper bounds of the confidence intervals 
+	(with the column names indicating the \code{level} of the interval.)
+	
+	\item{vcov}{: The variance-covariance matrix of the parameters.}
+	
+	\item{elements}{: Vector of length \code{npar(object)} indicating which elements of 
+	the parameter vector are included in computing the hessian, the variance-covariance matrix, 
+	the standard errors and/or the confidence intervals.}
+	
+	
+		\item{inc}{: 'inc'luded parameter.}
+		\item{fix}{: 'fix'ed parameter.}
+		\item{bnd}{: parameter estimated on the boundary.}
+	
+	\item{par}{: The values of the parameters.}
+	
+	\item{se}{: The values of the standard errors of the parameters.}
+	
+	\item{lower/upper}{: The lower and upper bounds of the confidence intervals; column names
+	indicate the as in 0.5+/-level/2, using the \code{level} argument.}
+	
+}
+
+\note{
+	
+	Note that the quality of the resulting standard errors is similar to those reported in 
+	Visser et al (2000) for both bootstrap and the profile likelihood methods. In Visser et
+	al (2000), the finite differences standard errors were somewhat less precise as they 
+	relied on a very parsimonious but indeed less precise method for computing the finite
+	differences approximation (computation time was a much scarcer resource at the time then
+	it is now). 
+	
+}
+
+\references{ 	
+
+	Ingmar Visser, Maartje E. J. Raijmakers, and Peter C. M. Molenaar (2000).
+	Confidence intervals for hidden Markov model parameters. \emph{British journal
+	of mathematical and statistical psychology}, 53, p. 317-327. 
+
+}
+
+
+\examples{
+
+	data(speed)
+
+	# 2-state model on rt and corr from speed data set 
+	# with Pacc as covariate on the transition matrix
+	# ntimes is used to specify the lengths of 3 separate series
+	mod1 <- depmix(list(rt~1,corr~1),data=speed,transition=~Pacc,nstates=2,
+		family=list(gaussian(),multinomial("identity")),ntimes=c(168,134,137))
+	
+	# fit the model
+	set.seed(3)
+	fmod1 <- fit(mod1)
+	
+	vcov(fmod1)$vcov # $
+	standardError(fmod1)
+	confint(fmod1)
+	
+}
+
+\author{ Ingmar Visser }
+
+\keyword{ methods }
+



More information about the depmix-commits mailing list