Hi list, I would like to use the following data.frame to generate matrices
over a 3 year moving window:
DF = 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
84 8026 1999
84 8027 1999
85 8028 1995
85 8029 1998"),head=TRUE,stringsAsFactors=FALSE))
Function to be applied: t(as.matrix(table(DF[,1:2]))) %*%
as.matrix(table(DF[,1:2]))
I tried this without success:
n<-rollapply(DF, width = 3, FUN = t(as.matrix(table(DF[,1:2]))) %*%
as.matrix(table(DF[,1:2])), align = "right")
Any suggestions? Thanks a lot!
--
View this message in context: http://r.789695.n4.nabble.com/Create-matrices-for-time-series-tp3449005p3449005.html
Sent from the R help mailing list archive at Nabble.com.
Create matrices for time series
3 messages · Gabor Grothendieck, mathijsdevaan
On Thu, Apr 14, 2011 at 3:51 AM, mathijsdevaan <mathijsdevaan at gmail.com> wrote:
Hi list, I would like to use the following data.frame to generate matrices
over a 3 year moving window:
DF = 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
84 ?8026 ?1999
84 ?8027 ?1999
85 ?8028 ?1995
85 ?8029 ?1998"),head=TRUE,stringsAsFactors=FALSE))
Function to be applied: t(as.matrix(table(DF[,1:2]))) %*%
as.matrix(table(DF[,1:2]))
I tried this without success:
n<-rollapply(DF, width = 3, FUN = t(as.matrix(table(DF[,1:2]))) %*%
as.matrix(table(DF[,1:2])), align = "right")
Try this: Lines <- " 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 84 8026 1999 84 8027 1999 85 8028 1995 85 8029 1998" DF <- read.table(textConnection(Lines), header = TRUE) f <- function(y) crossprod(table(DF[DF$C %in% y, 1:2])) years <- sort(unique(DF$C)) e <- as.data.frame(embed(years, 3)) lapply(split(e, e[, 1]), f)
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
3 days later
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.