[Depmix-commits] r499 - pkg/depmixS4/man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Oct 27 15:34:57 CEST 2011


Author: ingmarvisser
Date: 2011-10-27 15:34:56 +0200 (Thu, 27 Oct 2011)
New Revision: 499

Modified:
   pkg/depmixS4/man/depmix.Rd
   pkg/depmixS4/man/responses.Rd
Log:
Added several examples: Poisson switching data (generated data), S & P 500 returns data, multinomial data with n>1 and different for different time points (artificial data).

Modified: pkg/depmixS4/man/depmix.Rd
===================================================================
--- pkg/depmixS4/man/depmix.Rd	2011-10-26 15:36:23 UTC (rev 498)
+++ pkg/depmixS4/man/depmix.Rd	2011-10-27 13:34:56 UTC (rev 499)
@@ -192,7 +192,66 @@
     family=list(gaussian(),multinomial("identity")),ntimes=c(168,134,137))
 # print the model, formulae and parameter values
 mod
+set.seed(1)
+# fit the model by calling fit
+fm <- fit(mod)
 
+# this creates data with a single change point with Poisson data
+set.seed(3)
+y1 <- rpois(50,1)
+y2 <- rpois(50,2)
+ydf <- data.frame(y=c(y1,y2))
+
+# fit models with 1 to 3 states
+m1 <- depmix(y~1,ns=1,family=poisson(),data=ydf)
+set.seed(1)
+fm1 <- fit(m1)
+m2 <- depmix(y~1,ns=2,family=poisson(),data=ydf)
+set.seed(1)
+fm2 <- fit(m2)
+m3 <- depmix(y~1,ns=3,family=poisson(),data=ydf)
+set.seed(1)
+fm3 <- fit(m3,em=em.control(maxit=500))
+
+# plot the BICs to select the proper model
+plot(1:3,c(BIC(fm1),BIC(fm2),BIC(fm3)),ty="b")
+
+# similar to the binomial model, data may also be entered in multi-column format where the n for each
+# row can be different
+dt <- data.frame(y1=c(0,1,1,2,4,5),y2=c(1,0,1,0,1,0),y3=c(4,4,3,2,1,1))
+# specify a mixture model ...
+m2 <- mix(cbind(y1,y2,y3)~1,data=dt,ns=2,family=multinomial("identity"))
+set.seed(1)
+fm2 <- fit(m2)
+# ... or dependent mixture model
+dm2 <- depmix(cbind(y1,y2,y3)~1,data=dt,ns=2,family=multinomial("identity"))
+set.seed(1)
+fdm2 <- fit(dm2)
+
+
+# Volatility of S & P 500 returns
+# (thanks to Chen Haibo for providing this example)
+
+require(TTR)
+
+# load SP500 returns
+Sys.setenv(tz='UTC')
+
+sp500 <- getYahooData('^GSPC',start=19500101,end=20110731,freq='daily')
+ep <- endpoints(sp500, on="months", k=1)
+sp500 <- sp500[ep[2:(length(ep)-1)]]
+sp500$sp500_ret <- log(sp500$Close) - lag(log(sp500$Close))
+sp500 <- na.exclude(sp500)
+
+# fit some models
+mod <- depmix(sp500_ret~1,nstates=2,data=sp500)
+set.seed(2)
+fm2 <- fit(mod)	
+
+# plot BIC and posterior state sequence for the 2-state model
+plot(as.ts(posterior(fm2)[,3]),ylab="probability",main="Posterior probability of state 2 (volatile, negative markets).")
+
+
 }
 
 \keyword{methods}

Modified: pkg/depmixS4/man/responses.Rd
===================================================================
--- pkg/depmixS4/man/responses.Rd	2011-10-26 15:36:23 UTC (rev 498)
+++ pkg/depmixS4/man/responses.Rd	2011-10-27 13:34:56 UTC (rev 499)
@@ -131,6 +131,13 @@
 	fit(mod)
 	colSums(mod at y[which(x==0),])/length(which(x==0))
 	colSums(mod at y[which(x==1),])/length(which(x==1))
+	# note that the response is treated as factor here, internal representation is in dummy coded format:
+	head(mod at y)
+	# similar to the binomial model, data may also be entered in multi-column format where the n for each
+	# row can be different
+	dt <- data.frame(y1=c(0,1,1,2,4,5),y2=c(1,0,1,0,1,0),y3=c(4,4,3,2,1,1))
+	m2 <- mix(cbind(y1,y2,y3)~1,data=dt,ns=2,family=multinomial("identity"))
+	fm2 <- summary(fm2)
 	
 	# multivariate normal response model
 	mn <- c(1,2,3)
@@ -154,6 +161,22 @@
 	fit(mod)
 	glm(res~x, family=poisson)
 	
+	# this creates data with a single change point with Poisson distributed data
+	set.seed(3)
+	y1 <- rpois(50,1)
+	y2 <- rpois(50,2)
+	ydf <- data.frame(y=c(y1,y2))
+	
+	# fit models with 1 to 3 states
+	m1 <- depmix(y~1,ns=1,family=poisson(),data=ydf)
+	fm1 <- fit(m1)
+	m2 <- depmix(y~1,ns=2,family=poisson(),data=ydf)
+	fm2 <- fit(m2)
+	m3 <- depmix(y~1,ns=3,family=poisson(),data=ydf)
+	fm3 <- fit(m3,em=em.control(maxit=500))
+	
+	# plot the BICs to select the proper model
+	plot(1:3,c(BIC(fm1),BIC(fm2),BIC(fm3)),ty="b")
 }
 	
 \author{Maarten Speekenbrink & Ingmar Visser}



More information about the depmix-commits mailing list