Re: [R] Create matrices for time series

From: mathijsdevaan <mathijsdevaan_at_gmail.com>
Date: Mon, 18 Apr 2011 01:57:34 -0700 (PDT)

Thanks a lot!

Given the calculations below, I would like to generate a data.frame in which the value of case 8029 (i) in 1998 = sum (element c(i,j) * element final1(1,j) * 1st eigenvalue 1998 * element final2 (i,j)) * (1/sum final2(i)) in which j are all other cases in year 1998 and i IS NOT j? so for 8029 in 1998 = (0.9072184 * -0.5093407 * 3.64178798 * 0 + 0.8733873 *
-0.4763885 * 3.64178798 * 1 + 0.9058216 * -0.5072112 * 3.64178798 * 1) *
(1/3) = -1.06281276

Using this formula, I would like to generate a list per year (1997 - 1999) with all unique cases (1998 = 8025, 8026, 8027, 8029) in that year and the outcome of the formula.

I've been trying to run this formula, but I am having a hard time connecting each element to each other element. Could anyone please help me? Thanks in advance!  

library(zoo)

DF1 = data.frame(read.table(textConnection(" B C D E F G 8025 1995 0 4 1 2
8025 1997 1 1 3 4
8026 1995 0 7 0 0
8026 1996 1 2 3 0
8026 1997 1 2 3 1
8026 1998 6 0 0 4
8026 1999 3 7 0 3
8027 1997 1 2 3 9
8027 1998 1 2 3 1
8027 1999 6 0 0 2
8028 1999 3 7 0 0
8029 1995 0 2 3 3
8029 1998 1 2 3 2
8029 1999 6 0 0 1"),head=TRUE,stringsAsFactors=FALSE))

a <- read.zoo(DF1, split = 1, index = 2, FUN = identity) sum.na <- function(x) if (any(!is.na(x))) sum(x, na.rm = TRUE) else NA b <- rollapply(a, 3, sum.na, align = "right", partial = TRUE) newDF <- lapply(1:nrow(b), function(i)

       prop.table(na.omit(matrix(b[i,], nc = 4, byrow = TRUE,
               dimnames = list(unique(DF1$B), names(DF1)[-1:-2]))), 1))
names(newDF) <- time(a)
c<-lapply(newDF, function(mat) tcrossprod(mat / sqrt(rowSums(mat^2)))) d <- lapply(c, function(x) eigen(x, symmetric = TRUE, only.values = FALSE, EISPACK = FALSE)) DF2 = data.frame(read.table(textConnection(" A B C 80 8025 1995
80 8026 1995
80 8029 1995
81 8026 1996
82 8025 1997
82 8026 1997
83 8025 1997
83 8027 1997
90 8026 1998
90 8027 1998
90 8029 1998
84 8026 1999
84 8027 1999
85 8028 1999
85 8029 1999"),head=TRUE,stringsAsFactors=FALSE))

e <- function(y) crossprod(table(DF2[DF2$C %in% y, 1:2])) years <- sort(unique(DF2$C))
f <- as.data.frame(embed(years, 3))
g<-lapply(split(f, f[, 1]), e)
library(sna)
h<-event2dichot(g, method="absolute", thresh=0.99)

--

View this message in context: http://r.789695.n4.nabble.com/Create-matrices-for-time-series-tp3449005p3456958.html Sent from the R help mailing list archive at Nabble.com.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 18 Apr 2011 - 10:35:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 18 Apr 2011 - 10:40:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive