Ignore last reply. I sent the wrong script. > set.seed(1001) > > # Raw data in long format -- > raw <- data.frame(name=c("f1","f1","f1","f1","f2","f2","f2","f2"), + year=c(83, 84, 85, 86, 83, 84, 85, 86), + state=sample(1:3, 8, replace=TRUE) + ) > # Shift to wide format -- > fixedup <- reshape(raw, timevar="year", idvar="name", v.names="state", + direction="wide") > > trans <- as.matrix(fixedup) > result <- NULL > # loop through all the columns and build up the 'result' > for (i in 2:(ncol(trans) - 1)){ + result <- rbind(result, cbind(name=trans[,1], PREV=trans[,i], NEXT=trans[,i+1])) + } > result name PREV NEXT 1 "f1" "3" "2" 5 "f2" "2" "3" 1 "f1" "2" "2" 5 "f2" "3" "1" 1 "f1" "2" "2" 5 "f2" "1" "1" > > (markov <- table(result[,"PREV"], result[,"NEXT"]))
1 2 3 1 1 0 0 2 0 2 1 3 1 1 0 On 1/21/06, jim holtman <[EMAIL PROTECTED]> wrote: > > Is this what you want: > > > set.seed(1001) > > # Raw data in long format -- > raw <- data.frame(name=c("f1","f1","f1","f1","f2","f2","f2","f2"), > year=c(83, 84, 85, 86, 83, 84, 85, 86), > state=sample(1:3, 8, replace=TRUE) > ) > # Shift to wide format -- > fixedup <- reshape(raw, timevar="year", idvar="name", v.names="state", > direction="wide") > > trans <- as.matrix(fixedup) > result <- NULL > for (i in 2:(ncol(trans) - 1)){ > result <- rbind(result, cbind(name=trans[,1], prev=trans[,i], > next=trans[,i+1])) > } > > result > > markov <- table(try$prev.state, try$new.state) > > > > > > On 1/21/06, Ajay Narottam Shah <[EMAIL PROTECTED]> wrote: > > > > Folks, > > > > I am holding a dataset where firms are observed for a fixed (and > > small) set of years. The data is in "long" format - one record for one > > firm for one point in time. A state variable is observed (a factor). > > > > I wish to make a markov transition matrix about the time-series > > evolution of that state variable. The code below does this. But it's > > hardcoded to the specific years that I observe. How might one > > generalise this and make a general function which does this? :-) > > > > -ans. > > > > > > > > set.seed(1001) > > > > # Raw data in long format -- > > raw <- data.frame(name=c("f1","f1","f1","f1","f2","f2","f2","f2"), > > year=c(83, 84, 85, 86, 83, 84, 85, 86), > > state=sample(1:3, 8, replace=TRUE) > > ) > > # Shift to wide format -- > > fixedup <- reshape(raw, timevar="year", idvar="name", v.names="state", > > direction="wide") > > # Now tediously build up records for an intermediate data structure > > try <- rbind( > > data.frame(prev=fixedup$state.83, new=fixedup$state.84), > > data.frame(prev=fixedup$state.84, new=fixedup$state.85), > > data.frame(prev=fixedup$state.85, new=fixedup$state.86) > > ) > > # This is a bad method because it is hardcoded to the specific values > > # of "year". > > markov <- table(destination$prev.state, destination$new.state) > > > > -- > > Ajay Shah http://www.mayin.org/ajayshah > > > > [EMAIL PROTECTED] > > http://ajayshahblog.blogspot.com > > <*(:-? - wizard who doesn't know the answer. > > > > ______________________________________________ > > R-help@stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > > > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 247 0281 > > What the problem you are trying to solve? -- Jim Holtman Cincinnati, OH +1 513 247 0281 What the problem you are trying to solve? [[alternative HTML version deleted]] ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html