<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><style><!--

@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}

p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}

@list l0
        {mso-list-id:1923365937;
        mso-list-type:hybrid;
        mso-list-template-ids:1703682434 67698705 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
        {mso-level-text:"%1\)";
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0in;}
ul
        {margin-bottom:0in;}
--></style></div><div>I don't know if there is an easier way, already implemented in the Traminer package, but a base-R solution would be:<br></div><div><br data-mce-bogus="1"></div><div>1. splitting your data - split() - and get the results in a list, each slot of the list storing one individual's sequences<br data-mce-bogus="1"></div><div>2. applying a function to each slot of the list with lapply() to compute the distances for each individual<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>If you do that, it is also very easy to compute with multiple cores using mclapply() instead of lapply().<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Regards,<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Hadrien<br data-mce-bogus="1"></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><b>De: </b>"Reynolds, Jeremy E" <reyno113@purdue.edu><br><b>À: </b>"traminer-users" <traminer-users@lists.r-forge.r-project.org><br><b>Envoyé: </b>Vendredi 7 Février 2020 19:05:01<br><b>Objet: </b>[Traminer-users] within dyad distances<br></div><div><br></div><div data-marker="__QUOTED_TEXT__">






<div class="WordSection1">
<p class="MsoNormal">Dear Traminer Users,</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">I would like to compute distances between sequences that belong to the same person for every person in my data. 
</p>
<p class="MsoNormal">The code below seems to work: it calculates the distances between the expected and actual work schedule for each of the 10 people in the data.</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">My code, however, is terribly inefficient.  For instance, it calculates the entire pairwise distance matrix and then overwrites most of it with NA.
</p>
<p class="MsoNormal">This leaves me with two questions:</p>
<p class="MsoNormal"> </p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l0 level1 lfo1"><span style="mso-list:Ignore">1)<span style="font:7.0pt "Times New Roman"">     
</span></span>Does Traminer have a way to calculate just the distances between sequences that below to the same person?
</p>
<p class="MsoListParagraph"> (e.g., with clever use of the refseq option in the seqdist command or with the seqdistmc command for multi-channel sequence analysis)</p>
<p class="MsoListParagraph"> </p>
<p class="MsoListParagraph" style="text-indent:-.25in;mso-list:l0 level1 lfo1"><span style="mso-list:Ignore">2)<span style="font:7.0pt "Times New Roman"">     
</span></span>Is there a way to extract the elements just below the diagonal without overwriting all the other values with NA?</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">Thanks,</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">Jeremy</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"><span lang="DE">mymat <- rbind(</span></p>
<p class="MsoNormal"><span lang="DE">    c(1,1,0,0,1,1,1,1,1,0,0,0),c(1,2,0,0,1,1,1,1,1,1,0,0),</span></p>
<p class="MsoNormal"><span lang="DE">    c(2,1,0,0,1,1,1,1,1,0,0,0),c(2,2,0,0,1,1,1,1,1,1,1,0),</span></p>
<p class="MsoNormal"><span lang="DE">    c(3,1,0,0,1,1,1,1,1,0,0,0),c(3,2,0,0,1,1,1,1,1,1,1,1),</span></p>
<p class="MsoNormal"><span lang="DE">    c(4,1,0,0,1,1,1,1,1,0,0,0),c(4,2,0,1,1,1,1,1,1,0,0,0),</span></p>
<p class="MsoNormal"><span lang="DE">    c(5,1,0,0,1,1,1,1,1,0,0,0),c(5,2,1,1,1,1,1,1,1,0,0,0),</span></p>
<p class="MsoNormal"><span lang="DE">    c(6,1,0,0,1,1,1,1,1,0,0,0),c(6,2,0,0,0,1,1,1,1,0,0,0),</span></p>
<p class="MsoNormal"><span lang="DE">    c(7,1,0,0,1,1,1,1,1,0,0,0),c(7,2,0,0,0,0,1,1,1,0,0,0),</span></p>
<p class="MsoNormal"><span lang="DE">    c(8,1,0,0,1,1,1,1,1,0,0,0),c(8,2,0,0,0,0,0,1,1,0,0,0),</span></p>
<p class="MsoNormal"><span lang="DE">    c(9,1,0,0,1,1,1,1,1,0,0,0),c(9,2,0,0,0,0,0,0,1,0,0,0),</span></p>
<p class="MsoNormal"><span lang="DE">    c(10,1,0,0,1,1,1,1,1,0,0,0),c(10,2,0,0,0,0,0,0,0,0,0,0)</span></p>
<p class="MsoNormal"><span lang="DE">    )</span></p>
<p class="MsoNormal"><span lang="DE"> </span></p>
<p class="MsoNormal"><span lang="DE">colnames(mymat) <- c("ID", "sched", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9", "t10")</span></p>
<p class="MsoNormal">mymat <- as.data.frame(mymat)</p>
<p class="MsoNormal">mymat$sched <- factor(mymat$sched,levels = c(1,2), labels = c("Expected", "Actual"))</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">library(TraMineR)</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"># make sequence object</p>
<p class="MsoNormal">labels <- c("working", "not working")</p>
<p class="MsoNormal"><span lang="DE">scode <- c("W", "N")</span></p>
<p class="MsoNormal"><span lang="DE">seq <- seqdef(mymat, 3:12, states = scode, labels = labels)</span></p>
<p class="MsoNormal"><span lang="DE"> </span></p>
<p class="MsoNormal"># sequence index plot</p>
<p class="MsoNormal">seqIplot(seq, with.legend = T, main = "Expected and Actual Work Schedules of 5 People",border = NA)</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"># calculate dynamic hamming distances for every possible pair</p>
<p class="MsoNormal">distmat <- seqdist(seq, method = "DHD", indel = 1, sm = NULL)</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"># extract the elements just below the main diagonal</p>
<p class="MsoNormal">low <- 1</p>
<p class="MsoNormal">high <- 1</p>
<p class="MsoNormal">delta <- row(distmat) - col(distmat)</p>
<p class="MsoNormal">distmat[delta < low | delta > high] <- NA</p>
<p class="MsoNormal">distvec <- na.omit(as.data.frame(distmat[delta >= low | delta <= high]))</p>
<p class="MsoNormal">#repeat the last entry</p>
<p class="MsoNormal">distvec <- rbind(distvec,tail(distvec, n=1))</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">#attach the off diagonal to the data frame</p>
<p class="MsoNormal">colnames(distvec) <- "dist"</p>
<p class="MsoNormal">mymat <- cbind(mymat,distvec)</p>
<p class="MsoNormal"> </p>
<div style="mso-element:para-border-div;border:none;border-bottom:solid windowtext 1.5pt;padding:0in 0in 1.0pt 0in">
<p class="MsoNormal" style="border:none;padding:0in"> </p>
</div>
<p class="MsoNormal">Dr. Jeremy Reynolds</p>
<p class="MsoNormal">Professor</p>
<p class="MsoNormal">307 Stone Hall  </p>
<p class="MsoNormal">Department of Sociology</p>
<p class="MsoNormal">700 W. State Street</p>
<p class="MsoNormal">Purdue University </p>
<p class="MsoNormal">West Lafayette, IN 47907 </p>
<p class="MsoNormal">Phone: (765) 496-3348 </p>
<p class="MsoNormal"><a href="https://cla.purdue.edu/directory/profiles/jeremy-reynolds.html" target="_blank">https://cla.purdue.edu/directory/profiles/jeremy-reynolds.html</a><br data-mce-bogus="1"></p>
<p class="MsoNormal">Pronouns: he/him/his</p>
<p class="MsoNormal"> </p>
</div>


<br>_______________________________________________<br>Traminer-users mailing list<br>Traminer-users@lists.r-forge.r-project.org<br>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users<br></div></div></body></html>