[R] compare two matrices

Rui Barradas rui1174 at sapo.pt
Tue Apr 10 19:19:20 CEST 2012


Hello,

> 
> In Reply To
> compare two matrices
> Apr 10, 2012; 9:26am — by Kehl Dániel Kehl Dániel
> Dear Members,
> 
> I have two estimated transition matrices and I want to compare them.
> In fact I want to check the hypothesis if they come from the same process.
> I tried to look for some test but all I found was independence test of
> contingency tables.
> 

There is a discussion on markov chain transition matrices in
http://www-sre.wu-wien.ac.at/ersa/ersaconfs/ersa02/cd-rom/papers/024.pdf

>
> The following code shows that the usual chi-squared test statistic does
> not follow chisq distribution.
> 

Like discussed in the paper above, you must multiply the ratio (M1-M2)^2/M2
by the frequencies,
and get an asymptotic chi-square distribution. More or less like this:


set.seed(1)
for (i in 1:MCRepl){
   n <- 10000

   t1 <- table(sample(1:9, size = n, replace = TRUE))
   M1 <- matrix(t1, 3, 3)
   M1b <- M1/rowSums(M1)
   t2 <- table(sample(1:9, size = n, replace = TRUE))
   M2 <- matrix(t2, 3, 3)
   M2b <- M2/rowSums(M2)

   khi12[i] <- sum(M1*(M1b-M2b)^2/M2b)
}

x <- seq(floor(min(khi12)), ceiling(max(khi12)), length=MCRepl)
y <- dchisq(x, df=prod(dim(M1b) - 1))
ylim <- range(c(y, density(khi12)$y))

dev.new()
hist(khi12, prob=TRUE, ylim=ylim)
lines(density(khi12))
lines(x, y, col="blue", lty="dotted")


The loop could be a bit simplified, I've kept M2 and M2b for clarity sake.

Hope this helps,

Rui Barradas




--
View this message in context: http://r.789695.n4.nabble.com/compare-two-matrices-tp4544922p4546339.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list