# Polyadic Sequence Analysis using Tim Liao's seqpolyads # Allison Dunatchik # QM Methodology Working Group # 7 December 2022 #install.packages("TraMineR") #install.packages("TraMineRextras") library(TraMineR) library(TraMineRextras) rm(list=ls()) # load the example data from the TraMineRextras library data(polyads) Gen <- polyads$Gen # Define each generation as sequences seqGrandP <- seqdef(polyads[Gen=="1st Generation",2:11]) seqParent <- seqdef(polyads[Gen=="2nd Generation",2:11]) seqChild <- seqdef(polyads[Gen=="3rd Generation",2:11]) Seq <- rbind(seqGrandP,seqParent,seqChild) # rename columns to correspond with age colnames(Seq) <- 21:30 # Sequence index plot by generation seqIplot(Seq,group=Gen,ylim=c(10,0),ylab="Triad",xlab="Age") # Sequence density plot seqdplot(Seq,group=Gen, with.legend =T) # Create list of each generation # (we'll use this later to look at specific dyadic relationships) seqL <- list(seqGrandP,seqParent,seqChild) # Dyadic SA - grandparent-parent generation (gen 1 and 2) dyads seqG2.Tim <- seqpolyads(seqL[1:2], # Specifies which generations to include method="HAM", # specify your distance measure here a=1, # specifies whether entire sequences or states are randomized (default is 1(sequneces)) core=1, # number of cores for computation T=1000) # Number of randomly generated polyads # you could also specify s = .... to set the seed, default is 36963 # Define sequence length (needed for chi2 distance) slgth <- ncol(Seq) seqG2.Dur <- seqpolyads(seqL[1:2], method="CHI2", step=slgth, # Parameter for chi2 distance measure; length of intervals core=1, T=1000) seqG2.Order <- seqpolyads(seqL[1:2], method="SVRspell", tpow=0, # Parameter for SVRspell distance measure; exponential weight of spell length kweights=1, # Parameter for SVRspell distance measure; weights applied to subsequences core=1, T=1000) # Polyadic SA - 3 generation triads seqG3.Tim <- seqpolyads(seqL, method="HAM", a=1, core=1, T=1000) seqG3.Dur <- seqpolyads(seqL, method="CHI2", step=slgth, core=1, T=1000) seqG3.Order <- seqpolyads(seqL, method="SVRspell", tpow=0, kweights=1, core=1, T=1000)