[Depmix-commits] r146 - in trunk: . tests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jun 10 14:34:29 CEST 2008
Author: ingmarvisser
Date: 2008-06-10 14:34:29 +0200 (Tue, 10 Jun 2008)
New Revision: 146
Added:
trunk/tests/depmixNew-test2.R
trunk/tests/depmixNew-test3.R
Removed:
trunk/depmixNew-test2.R
trunk/depmixNew-test3.R
Log:
Moved test files to test dir
Deleted: trunk/depmixNew-test2.R
===================================================================
--- trunk/depmixNew-test2.R 2008-06-10 12:33:38 UTC (rev 145)
+++ trunk/depmixNew-test2.R 2008-06-10 12:34:29 UTC (rev 146)
@@ -1,97 +0,0 @@
-
-#
-# Started by Ingmar Visser & Maarten Speekenbrink, march 2008
-#
-# Usage: go to trunk directory and source this file in R, if the program
-# still works it should return TRUE at every test (or make immediate sense
-# otherwise)
-
-#
-# test model with EM optimization, no covariates
-#
-
-require(depmixS4)
-
-data(speed)
-
-trstart=c(0.899,0.101,0.084,0.916)
-instart=c(0.5,0.5)
-resp <- c(5.52,0.202,0.472,0.528,6.39,0.24,0.098,0.902)
-
-mod <- depmix(list(rt~1,corr~1),data=speed,nstates=2,family=list(gaussian(),multinomial()),trstart=trstart)
-# respstart=resp,trstart=trstart,instart=instart)
-
-logLik(mod)
-
-mod1 <- fit(mod)
-
-ll <- logLik(mod1)
-
-#
-# Test optimization using Rdonlp2
-#
-
-trstart=c(0.899,0.101,0,0.01,0.084,0.916,0,0)
-instart=c(0.5,0.5)
-resp <- c(5.52,0.202,0.472,0.528,6.39,0.24,0.098,0.902)
-
-mod <- depmix(list(rt~1,corr~1),data=speed,transition=~Pacc,nstates=2,family=list(gaussian(),multinomial()),
- respstart=resp,trstart=trstart,instart=instart)
-
-logLik(mod)
-
-mod1 <- fit(mod)
-ll <- logLik(mod1)
-
-cat("Test 1: ", ll, "(loglike of speed data with covariate, hopefully better than 300.5701) \n")
-
-mod2 <- fit(mod,meth="EM")
-
-post <- cbind(viterbi(mod1),speed$Pacc)
-
-
-#
-# now constrain the initial state probs to be 0 and 1
-# also constrain the guessing probs to be 0.5 and 0.5
-#
-
-# set the starting values of this model to the optimized
-# values of the previously fitted model to speed optimization
-pars <- c(unlist(getpars(mod1)))
-# set some to other values, ie the ones that we want to constrain
-pars[2]=+Inf
-pars[14]=0
-
-mod <- setpars(mod,pars)
-
-# fix some parameters
-free <- c(0,0,rep(c(0,1),4),1,1,0,0,1,1,0,1)
-
-mod2 <- fit(mod,fixed=!free)
-
-llratio(mod1,mod2)
-
-cat("Test 2: likelihood ratio not significant, hence mod2 better than mod1.\n")
-
-#
-# NOW ADD SOME GENERAL LINEAR CONSTRAINTS
-#
-
-# set the starting values of this model to the optimized
-# values of the previously fitted model to speed optimization
-pars <- c(unlist(getpars(mod2)))
-mod <- setpars(mod,pars)
-
-# start with fixed and free parameters
-conpat <- c(0,0,rep(c(0,1),4),1,1,0,0,1,1,0,1)
-# constrain the beta's on the transition parameters to be equal
-conpat[4] <- conpat[8] <- 2
-conpat[6] <- conpat[10] <- 3
-
-mod3 <- fit(mod,equal=conpat)
-
-llratio(mod2,mod3)
-
-cat("Test3: log Likelihood ratio significant, constraints not tenable.")
-
-
Deleted: trunk/depmixNew-test3.R
===================================================================
--- trunk/depmixNew-test3.R 2008-06-10 12:33:38 UTC (rev 145)
+++ trunk/depmixNew-test3.R 2008-06-10 12:34:29 UTC (rev 146)
@@ -1,56 +0,0 @@
-
-#
-# Started by Ingmar Visser 26-2-2008
-#
-# Usage: go to trunk directory and source this file in R
-#
-
-#
-# BALANCE SCALE data example with age as covariate on class membership
-#
-
-require(depmixS4)
-
-data(balance)
-# four binary items on the balance scale task
-
-# now fit some latent class models
-trstart=c(1,0,0,1) # as this is a latent class model, the transition are not optimized
-instart=c(0.5,0.5)
-respstart=runif(16)
-# note that ntimes argument is used to make this a mixture model
-mod <- depmix(list(d1~1,d2~1,d3~1,d4~1), data=balance, nstates=2,
- family=list(multinomial(),multinomial(),multinomial(),multinomial()),
- respstart=respstart,trstart=trstart,instart=instart,
- ntimes=rep(1,nrow(balance)))
-
-mod1 <- fit(mod)
-
-# add age as covariate on class membership by using the prior argument
-trstart=c(1,0,0,1) # as this is a latent class model, the transition are not optimized
-instart=c(0.5,0.5,0,0) # we need the initial probs and the coefficients of age
-respstart=c(rep(c(0.1,0.9),4),rep(c(0.9,0.1),4))
-trstart=c(1,0,0,1)
-mod2 <- depmix(list(d1~1,d2~1,d3~1,d4~1), data=balance, nstates=2,
- family=list(multinomial(),multinomial(),multinomial(),multinomial()),
- trstart=trstart, instart=instart, respstart=respstart,
- ntimes=rep(1,nrow(balance)), prior=~age, initdata=balance)
-
-mod3 <- fit(mod2)
-
-llratio(mod3,mod1)
-
-
-#
-# fit these data as if the items were all parallel so the sumscore can be used as
-# a binomial with n=4 glm
-#
-
-# donlp optimzation instead
-
-# mod4 <- fit(mod2,fixed=fixed,method="donlp")
-
-
-
-
-
Copied: trunk/tests/depmixNew-test2.R (from rev 145, trunk/depmixNew-test2.R)
===================================================================
--- trunk/tests/depmixNew-test2.R (rev 0)
+++ trunk/tests/depmixNew-test2.R 2008-06-10 12:34:29 UTC (rev 146)
@@ -0,0 +1,97 @@
+
+#
+# Started by Ingmar Visser & Maarten Speekenbrink, march 2008
+#
+# Usage: go to trunk directory and source this file in R, if the program
+# still works it should return TRUE at every test (or make immediate sense
+# otherwise)
+
+#
+# test model with EM optimization, no covariates
+#
+
+require(depmixS4)
+
+data(speed)
+
+trstart=c(0.899,0.101,0.084,0.916)
+instart=c(0.5,0.5)
+resp <- c(5.52,0.202,0.472,0.528,6.39,0.24,0.098,0.902)
+
+mod <- depmix(list(rt~1,corr~1),data=speed,nstates=2,family=list(gaussian(),multinomial()),trstart=trstart)
+# respstart=resp,trstart=trstart,instart=instart)
+
+logLik(mod)
+
+mod1 <- fit(mod)
+
+ll <- logLik(mod1)
+
+#
+# Test optimization using Rdonlp2
+#
+
+trstart=c(0.899,0.101,0,0.01,0.084,0.916,0,0)
+instart=c(0.5,0.5)
+resp <- c(5.52,0.202,0.472,0.528,6.39,0.24,0.098,0.902)
+
+mod <- depmix(list(rt~1,corr~1),data=speed,transition=~Pacc,nstates=2,family=list(gaussian(),multinomial()),
+ respstart=resp,trstart=trstart,instart=instart)
+
+logLik(mod)
+
+mod1 <- fit(mod)
+ll <- logLik(mod1)
+
+cat("Test 1: ", ll, "(loglike of speed data with covariate, hopefully better than 300.5701) \n")
+
+mod2 <- fit(mod,meth="EM")
+
+post <- cbind(viterbi(mod1),speed$Pacc)
+
+
+#
+# now constrain the initial state probs to be 0 and 1
+# also constrain the guessing probs to be 0.5 and 0.5
+#
+
+# set the starting values of this model to the optimized
+# values of the previously fitted model to speed optimization
+pars <- c(unlist(getpars(mod1)))
+# set some to other values, ie the ones that we want to constrain
+pars[2]=+Inf
+pars[14]=0
+
+mod <- setpars(mod,pars)
+
+# fix some parameters
+free <- c(0,0,rep(c(0,1),4),1,1,0,0,1,1,0,1)
+
+mod2 <- fit(mod,fixed=!free)
+
+llratio(mod1,mod2)
+
+cat("Test 2: likelihood ratio not significant, hence mod2 better than mod1.\n")
+
+#
+# NOW ADD SOME GENERAL LINEAR CONSTRAINTS
+#
+
+# set the starting values of this model to the optimized
+# values of the previously fitted model to speed optimization
+pars <- c(unlist(getpars(mod2)))
+mod <- setpars(mod,pars)
+
+# start with fixed and free parameters
+conpat <- c(0,0,rep(c(0,1),4),1,1,0,0,1,1,0,1)
+# constrain the beta's on the transition parameters to be equal
+conpat[4] <- conpat[8] <- 2
+conpat[6] <- conpat[10] <- 3
+
+mod3 <- fit(mod,equal=conpat)
+
+llratio(mod2,mod3)
+
+cat("Test3: log Likelihood ratio significant, constraints not tenable.")
+
+
Copied: trunk/tests/depmixNew-test3.R (from rev 145, trunk/depmixNew-test3.R)
===================================================================
--- trunk/tests/depmixNew-test3.R (rev 0)
+++ trunk/tests/depmixNew-test3.R 2008-06-10 12:34:29 UTC (rev 146)
@@ -0,0 +1,56 @@
+
+#
+# Started by Ingmar Visser 26-2-2008
+#
+# Usage: go to trunk directory and source this file in R
+#
+
+#
+# BALANCE SCALE data example with age as covariate on class membership
+#
+
+require(depmixS4)
+
+data(balance)
+# four binary items on the balance scale task
+
+# now fit some latent class models
+trstart=c(1,0,0,1) # as this is a latent class model, the transition are not optimized
+instart=c(0.5,0.5)
+respstart=runif(16)
+# note that ntimes argument is used to make this a mixture model
+mod <- depmix(list(d1~1,d2~1,d3~1,d4~1), data=balance, nstates=2,
+ family=list(multinomial(),multinomial(),multinomial(),multinomial()),
+ respstart=respstart,trstart=trstart,instart=instart,
+ ntimes=rep(1,nrow(balance)))
+
+mod1 <- fit(mod)
+
+# add age as covariate on class membership by using the prior argument
+trstart=c(1,0,0,1) # as this is a latent class model, the transition are not optimized
+instart=c(0.5,0.5,0,0) # we need the initial probs and the coefficients of age
+respstart=c(rep(c(0.1,0.9),4),rep(c(0.9,0.1),4))
+trstart=c(1,0,0,1)
+mod2 <- depmix(list(d1~1,d2~1,d3~1,d4~1), data=balance, nstates=2,
+ family=list(multinomial(),multinomial(),multinomial(),multinomial()),
+ trstart=trstart, instart=instart, respstart=respstart,
+ ntimes=rep(1,nrow(balance)), prior=~age, initdata=balance)
+
+mod3 <- fit(mod2)
+
+llratio(mod3,mod1)
+
+
+#
+# fit these data as if the items were all parallel so the sumscore can be used as
+# a binomial with n=4 glm
+#
+
+# donlp optimzation instead
+
+# mod4 <- fit(mod2,fixed=fixed,method="donlp")
+
+
+
+
+
More information about the depmix-commits
mailing list