Dear Matthias,<br />Thank you very much for your help, which is extremely valuable.<br />I implemented the first solution, and instead of a random variable, I used a date I have in my dataset. For those interested, I put a copy of the protocol I used in attached file.<br /><br />Thank you again,<br />Simon<br /><br /><br />Matthias Studer <matthias.studer@unige.ch> wrote:<blockquote cite="http://entg2.sciences-po.fr/%3C4DDA0C84.7080004@unige.ch%3E" type="cite">
Hi Simon,<br />
<br />
I answer in English as other users may be interested by your
question. There are several ways to truncate sequences to a varying
length.<br />
<br />
Suppose we are working with the mvad data set and we would like to
truncate sequence to a random varying length<br />
<br />
## Creating the mvad sequence<br />
library(TraMineR)<br />
data(mvad)<br />
<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 />
mvad.seq <- seqdef(mvad, 17:86, alphabet = mvad.alphabet, states
= mvad.scodes,<br />
labels = mvad.labels, xtstep = 6)<br />
<br />
<br />
<br />
## Here we generate a random integer to cut the sequences<br />
## The results is a vector of length 712 with integer values ranging
from 10 to 69<br />
## This should be the length you would like to use to truncate your
sequences<br />
randomlength <- as.integer(runif(nrow(mvad))*60)+10<br />
head(randomlength)<br />
<br />
## On way is to use a "position" matrix, which store, for each cell
the current position in the sequence<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 "void" attribute
(i.e. end of sequence) to each position <br />
## that are greater than the truncating date<br />
mvad.seq[positionindex > randomlength] <- attr(mvad.seq,
"void")<br />
<br />
## Checking the results<br />
all.equal(as.numeric(seqlength(mvad.seq)), randomlength)<br />
<br />
## Plotting result<br />
seqiplot(mvad.seq)<br />
<br />
## Another way would be to use a loop <br />
## This may take much longer to compute<br />
## Personally, I prefer the previous solution<br />
<br />
mvad.seq <- seqdef(mvad, 17:86, alphabet = mvad.alphabet, states
= mvad.scodes,<br />
labels = mvad.labels, xtstep = 6)<br />
<br />
## For each sequence<br />
for(i in 1:length(randomlength)){<br />
## for the given position until the end assign the "void"
element<br />
## We should add one to randomlength to cut after the
randomlength<br />
mvad.seq[i, (randomlength[i]+1):ncol(mvad.seq)] <-
attr(mvad.seq, "void")<br />
}<br />
<br />
seqiplot(mvad.seq)<br />
## Checking the results<br />
all.equal(as.numeric(seqlength(mvad.seq)), randomlength)<br />
<br />
<br />
Hope this helps.<br />
<br />
Matthias Studer<br />
<br />
<br />
<br />
<br />
<br />
<br />
Le 19.05.2011 16:17, Simon PAYE a écrit :
<blockquote type="cite" cite="mid:31890729.39661.1305814646381.JavaMail.root@aten1.sciences-po.fr">
<style>
<!--
/* Font Definitions */
@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;}
/* Style Definitions */
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:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<!--[if gte mso 9]><xml>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:HyphenationZone>21</w:HyphenationZone>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
</w:Compatibility>
<w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel>
</w:WordDocument>
</xml><![endif]--><!--[if gte mso 9]><xml>
<w:LatentStyles DefLockedState="false" LatentStyleCount="156">
</w:LatentStyles>
</xml><![endif]-->
<style>
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {mso-style-parent:"";
        margin:0cm;
        margin-bottom:.0001pt;
        mso-pagination:widow-orphan;
        font-size:12.0pt;
        font-family:"Times New Roman";
        mso-fareast-font-family:"Times New Roman";}
@page Section1
        {size:595.3pt 841.9pt;
        margin:70.85pt 70.85pt 70.85pt 70.85pt;
        mso-header-margin:35.4pt;
        mso-footer-margin:35.4pt;
        mso-paper-source:0;}
div.Section1
        {page:Section1;}
-->
</style><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
        {mso-style-name:"Tableau Normal";
        mso-tstyle-rowband-size:0;
        mso-tstyle-colband-size:0;
        mso-style-noshow:yes;
        mso-style-parent:"";
        mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
        mso-para-margin:0cm;
        mso-para-margin-bottom:.0001pt;
        mso-pagination:widow-orphan;
        font-size:10.0pt;
        font-family:"Times New Roman";
        mso-ansi-language:#0400;
        mso-fareast-language:#0400;
        mso-bidi-language:#0400;}
</style>
<![endif]-->
<p class="MsoNormal">Bonjour chers collègues,</p>
<p class="MsoNormal"><o:p> </o:p></p><p> </p><p class="MsoNormal">Je fais irruption dans votre liste pour une
question qui me
travaille depuis quelque temps et qui potentiellement peut
intéresser beaucoup
d’analystes de séquences avec des durées variées (notamment les
carrières).</p>
<p class="MsoNormal"><o:p> </o:p></p><p> </p><p class="MsoNormal">Je dispose d’une base d’une centaine de
carrières d’universitaires
codées en STS d’une longueur de 5 à 47 années.</p>
<p class="MsoNormal">Pour mes analyses, j’ai besoin de les aligner
à droite (‘external
time reference’ ou ‘calendar time axis’), ou de les aligner à
gauche (‘internal
time reference’ ou ‘process time axis’). Jusqu’ici, pas de
problème, car je
peux passer d’un modèle à l’autre en utilisant les options
‘left’ et ‘right’ de
seqdef.</p>
<p class="MsoNormal"><o:p> </o:p></p><p> </p><p class="MsoNormal">Tout se complique lorsque je souhaite
tronquer les séquences
selon une date historique variable selon les individus. </p>
<p class="MsoNormal">Dans mon cas, c’est l’année d’obtention de la
tenure (emploi
permanent), que j’ai renseignée dans une variable appelée
‘year.tenure’. Si je
veux, par exemple, analyser les séquences menant à la tenure en
les alignant
toutes à droite selon l’année d’obtention de la tenure, comment
dois-je
procéder ?</p>
<p class="MsoNormal"><o:p> </o:p></p><p> </p><p class="MsoNormal">Je n’ai pas trouvé de solution dans le
‘user’s guide’, ni
dans les autres documents disponibles sur le site de TraMineR.</p>
<p class="MsoNormal"><o:p> </o:p></p><p> </p><p class="MsoNormal">Merci pour votre réponse et pour tout ce que
vous avez fait
jusque là,</p>
<p class="MsoNormal"><o:p> </o:p></p><p> </p><p class="MsoNormal">Simon</p>
<o:p> </o:p><br />
-- <br />
Simon Paye<br />
Doctorant en sociologie<br />
Centre de Sociologie des Organisations - Sciences Po Paris<br />
<br />
Tel: 0148741267<br />
<hr />
--------------------------------------------------------------------------
Tous
les courriers électroniques émis depuis la messagerie
de Sciences Po doivent respecter des conditions d'usages.
Pour les consulter rendez-vous sur
<a href="http://www.ressources-numeriques.sciences-po.fr/confidentialite_courriel.htm" class="moz-txt-link-freetext">http://www.ressources-numeriques.sciences-po.fr/confidentialite_courriel.htm</a>
<pre><fieldset class="mimeAttachmentHeader">
_______________________________________________
Traminer-users mailing list
<a href="mailto:Traminer-users@lists.r-forge.r-project.org" class="moz-txt-link-abbreviated">Traminer-users@lists.r-forge.r-project.org</a>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users" class="moz-txt-link-freetext">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/traminer-users</a>
</fieldset></pre>
</blockquote>
</blockquote></matthias.studer@unige.ch><HR>
--------------------------------------------------------------------------
Tous les courriers électroniques émis depuis la messagerie
de Sciences Po doivent respecter des conditions d'usages.
Pour les consulter rendez-vous sur
http://www.ressources-numeriques.sciences-po.fr/confidentialite_courriel.htm