<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<link href="chrome://translator/skin/floatingPanel.css"
type="text/css" rel="stylesheet">
<link href="chrome://translator/skin/popup.css" type="text/css"
rel="stylesheet">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Hi Simon,<br>
<br>
Sorry for taking so much time to answer your question... In this
answer, I will reuse the same code as the one used to truncate the
sequence to a varying length (available here:
<a class="moz-txt-link-freetext" href="http://lists.r-forge.r-project.org/pipermail/traminer-users/2011-May/000070.html">http://lists.r-forge.r-project.org/pipermail/traminer-users/2011-May/000070.html</a>
)<br>
<br>
Below, I give two examples to answer your question based on the mvad
dataset. In the first one, we will align the sequence according to a
random date specific to each individual. In the second one, we will
compute the age at first joblessness episode to align the sequence.<br>
<br>
<br>
## loading TraMineR and the data<br>
library(TraMineR)<br>
data(mvad)<br>
<br>
## Defining alphabet and so on<br>
mvad.alphabet <- c("employment", "FE", "HE", "joblessness",
"school",<br>
"training")<br>
mvad.labels <- c("employment", "further education", "higher
education",<br>
"joblessness", "school", "training")<br>
mvad.scodes <- c("EM", "FE", "HE", "JL", "SC", "TR")<br>
<br>
## The idea is to transform the data before defining the sequence
object<br>
## Here we store the sequence data in a the mvad.data object<br>
mvad.data <- mvad[, 17:86]<br>
<br>
<br>
## Here we generate a random integer to align the sequences<br>
## The results is a vector of length 712 with integer values ranging
from 1 to 20<br>
## This should be the date you want to use to align your sequences<br>
randomlength <- ceiling(runif(nrow(mvad))*20)<br>
head(randomlength)<br>
<br>
## On way is to use a "position" matrix, which store, for each cell
the current position in the sequence (see the previous mail about
truncating sequences)<br>
positionindex <- matrix(1:70, nrow=nrow(mvad), ncol=70,
byrow=TRUE)<br>
<br>
head(positionindex)<br>
<br>
## Using this position matrix, we can affect the NA value to each
position<br>
## that are before the aligning date<br>
mvad.data[positionindex < randomlength] <- NA<br>
<br>
## Now, here is the trick. We delete the NA values appearing on the
left using the option left="DEL"<br>
<br>
mvad.seq <- seqdef(mvad.data, alphabet = mvad.alphabet, states =
mvad.scodes,<br>
labels = mvad.labels, xtstep = 6, left="DEL")<br>
<br>
<br>
all.equal(as.numeric(seqlength(mvad.seq)), 71-randomlength)<br>
seqdplot(mvad.seq)<br>
<br>
## Second example, we align the sequences with the age at first
joblessness episode<br>
### Start by retrieving this age<br>
<br>
## Use a fresh data object<br>
mvad.data <- mvad[, 17:86]<br>
<br>
## Creating a vector containing by default NA values<br>
age <- rep(NA, nrow(mvad.data))<br>
<br>
state <- "joblessness"<br>
## for each column<br>
for(i in 1:ncol(mvad.data)){<br>
## Asign current column value to individual who first enter the
state "state"<br>
## To do that search for individuals that did not experience the
state before<br>
## Those individuals have NA entries in the age vector<br>
notexperienced <- is.na(age)<br>
## Select the individual that are now in the state "state"<br>
instate <- mvad.data[, i] == state<br>
## Assign current column value to individuals that meet both
conditions<br>
age[notexperienced & instate ] <- i<br>
}<br>
<br>
<br>
<br>
## indiv with NA values in age never experienced the state
joblessness, so remove it.<br>
<br>
mvad.dataJL <- mvad.data[!is.na(age), ]<br>
ageJL <- age[!is.na(age)]<br>
<br>
## Use the position matrix<br>
positionindex <- matrix(1:70, nrow=nrow(mvad.dataJL), ncol=70,
byrow=TRUE)<br>
<br>
## Using this position matrix, we can affect the NA value to each
position<br>
## that are before the aligning date<br>
mvad.dataJL[positionindex < ageJL] <- NA<br>
<br>
## Now, here is the trick. We delete the NA values appearing on the
left using the option left="DEL"<br>
<br>
mvad.seqJL <- seqdef(mvad.dataJL, alphabet = mvad.alphabet,
states = mvad.scodes,<br>
labels = mvad.labels, xtstep = 6, left="DEL")<br>
<br>
seqdplot(mvad.seqJL)<br>
<br>
Hope this helps.<br>
<br>
Matthias Studer<br>
<br>
Le 25.07.2011 18:42, Simon PAYE a écrit :
<blockquote
cite="mid:441733.10734.1311612125550.JavaMail.root@aten1.sciences-po.fr"
type="cite">
<pre wrap="">Dear colleagues,
I come back to the TraMineR list for another technical question that might be of use for other career or time-use analysts.
My purpose is to align all the sequences of my sequence object according to a date specific to each individual. The variable indicating the date is exogenous to the sequence. In my case, I would like to align careers according to the moment in which a specific turning point occurs (for example being given job tenure).
Example: aligning two sequences according to the date of tenure award
2000 2001 2002 2003 2004 2005
X A A B B B C
Y A A B B
(X got married in 2002 and Y in 2003)
I intend to end up with this:
T-2 T-1 Ten. T+1 T+2 T+3
X A A B B B C
Y A A B B
I have already aligned sequences manually in Excel, but it is a rather frustrating task. As I would like to do this aligning operation for various turning points in the career (e.g. parenthood, major publication, etc), I hope that we can succesfully develop a small loop in TraMineR or perhaps a new argument in the sequIplot function, something like:
R > seqdef(mydata, var = 81:127, align = mydata$year.tenure)
All the best and thank you again,
Simon
--------------------------------------------------------------------------
Tous les courriers électroniques émis depuis la messagerie
de Sciences Po doivent respecter des conditions d'usages.
Pour les consulter rendez-vous sur
<a class="moz-txt-link-freetext" href="http://www.ressources-numeriques.sciences-po.fr/confidentialite_courriel.htm">http://www.ressources-numeriques.sciences-po.fr/confidentialite_courriel.htm</a>
_______________________________________________
Traminer-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Traminer-users@lists.r-forge.r-project.org">Traminer-users@lists.r-forge.r-project.org</a>
<a class="moz-txt-link-freetext" href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users</a>
</pre>
</blockquote>
<div style="bottom: auto; left: 756px; right: auto; top: 137px;
display: none;" class="translator-theme-default"
id="translator-floating-panel">
<div title="Cliquer pour traduire"
id="translator-floating-panel-button"></div>
</div>
<div style="top: auto; max-width: 400px; bottom: 0pt; left: auto;
right: 0pt; display: none;" class="translator-theme-default"
id="translator-popup">
<div id="translator-popup-toolbar">
<div id="translator-popup-title">
<div id="translator-popup-source-languages-wrapper">
<ul style="margin-top: 0px;"
id="translator-popup-source-languages">
<li code="auto" class="translator-popup-language-detected">Anglais
- détecté</li>
<li code="en">Anglais</li>
<li code="fr">Français</li>
<li code="de">Allemand</li>
</ul>
<div class="translator-popup-languages-scroller"></div>
</div>
<div id="translator-popup-languages-direction"></div>
<div id="translator-popup-target-languages-wrapper">
<ul style="margin-top: -18px;"
id="translator-popup-target-languages">
<li code="en">Anglais</li>
<li code="fr">Français</li>
<li code="de">Allemand</li>
</ul>
<div class="translator-popup-languages-scroller"></div>
</div>
</div>
<div class="translator-popup-toolbar-spring"></div>
<a title="Copier le texte sélectionné dans le presse-papiers"
href="javascript:void(0);" id="translator-popup-button-copy"></a></div>
<div style="max-height: 480px;"
class="translator-message-type-normal"
id="translator-popup-message"></div>
<div style="display: none;" id="translator-popup-notice"></div>
<textarea style="display: none;" id="translator-popup-textarea"></textarea></div>
<div style="bottom: 0pt; left: auto; right: 0pt; top: auto; display:
none;" title="Traduction en cours..."
class="translator-theme-default" id="translator-popup-loading"></div>
</body>
</html>