intersect interval of time by a character variable - r

I have a dataset with worker curriculum with:
2 time variable: df$from and df$to
1 name variable: df$name
1 bureau variable: df$bureau
I would like to generate a varible df$varable which sum 1 point every time there is an intersection of time for different person in the same bureau.
I am able to do intersect(df$from, df$to) but not to condition on df$bureau.

Related

tmerge to split data into 5 yearly intervals

I have a data set with columns: ID, follow up (days), event, variable 1, variable 2 ..
I'm trying to use tmerge to convert into 5 yearly intervals with tstart and tstop using follow up (days) as time - how do I go about this?

Turn date column into days from beginning integer Rstudio

Hi everyone so I am currently plotting time series graphs in Rstudio, I have created a nice time series graph however I would actually like the x axis not to be showing me the date but more like an integer showing a number from the starting date of the graph.
Time Series Graph
Such as instead of seeing 01/01/2021 I want to see day 100, as in its the 100th day of recording data.
Do i need to create another column converting all the days into a numerical value then plot this?
If so how do i do this. At the moment all i have is a Date column and the value i am plotting column.
Column Data
Thanks
Assuming you want 01/01/2021 as first day you could use that as a reference and calculate the number of days passed since the first day of recording and plot that, this should give you more like an integer showing a number from the starting date.
Not sure what your data frame looks like so hopefully this helps.
Using lubridate
library(lubridate)
df
Date
1 01/01/2021
2 02/01/2021
3 03/01/2021
4 04/01/2021
df$days <- yday(dmy(df$Date)) -1
Output:
Date days
1 01/01/2021 0
2 02/01/2021 1
3 03/01/2021 2
4 04/01/2021 3
Which is indeed a numeric
str(df$days)
num [1:4] 0 1 2 3
This a simulation of dates
date.simulation = as.Date(1:100, "2001-01-01")
factor(date.simulation-min(date.simulation))
You just subtract the dates to the minimum date. And you need it as a factor for plotting purposes.

R apply linear regression by ID and date, and extract slope parameters for each ID x date

I have data organised by the following columns:
ID, Date, Time, Reading
Each ID has a series of readings (measurements) per date.
E.g.
ID 149 on 1/1/2020 has readings at times 1,2,3,4...
ID 149 on 2/1/2020 (or possibly 1/2/2020 if you're in the US) has readings at times 1,2,3,4...
ID 150 on 1/1/2020 has readings at times 1,2,3,4...
and so on
I want to run a linear regression of Reading ~ Time for each ID x Date combo, and extract the slope parameters from the regressions (maybe in a new data frame that keeps the ID and Date columns to be able to identify which regression each slope parameter came from). I'm very new to R, and wondering what the simplest method to try would be.

Transition matrix based on amounts

I have the following dataset:
example_data <- data.table(ID = c(1,2,3,4,4,6,1,2,3,4,4,5,1,2,3,4),
ContractID = c(1,2,3,4,5,6,1,2,3,4,5,7,10,11,12,13),
Day = c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3),
Rating = c("A","B","A","A","A","C","A","A","C","A","D","C","A","B","C","D"),
Amount = c(79,71,74,10,20,30,18,91,33,59,90,32,5,71,79,60))
and what I would like to create is a transition matrix from day 1 to day 2 and day 2 to day 3.
It would look like something like this:
Of course, the labels Day 1 and Day 2 are for illustrative purposes only.
For example: cell A, C with the value -41, we get it by 33 - 74, since the amount decreased from day 1 to day 2.
Observe that there are some small caveats:
We consider only contractIDs and IDs that are present in Day 1 and Day 2.
The contractID and ID are unique together and not only the ID.
I was wondering how one would be able to create such a table, without actually splitting it into two datasets, for the two days.

Handling SPELL data with exact dates (feature request?)

I'm learning TraMineR and have used different types of longitudinal data. My original data is SPELL data with id, start time, end time and status, where the start and end times are exact dates, so my subsequences have varying lengths
With seqformat() I can chop the data (automatically) into 1 year pieces and convert into STS format, eg. where first variable is the first date, second variable is the first date + 1 year and so on.
What I would like to do is adjust the conversion so that I could use half year or one month time periods.
Here I have converted the dates into years with decimals with decimal.date():
id start end status
1 1 1965.138 1965.974 1
2 1 1968.714 1987.237 1
3 1 1985.667 2003.933 2
4 1 1988.499 1988.665 1
5 1 1996.652 1996.878 1
The sequence object that is created automatically has the data in one year subsequences:
$ y1960.16803278689
$ y1961.16803278689
$ y1962.16803278689
$ y1963.16803278689
So with data with dates I would like to have the option to use also shorter than 1 year subsequence lengths. I understand that with seqgranularity() the opposite is possible.
Alternatively I'm interested to know if there's some way in R outside TraMineR to handle the SPELL data to create certain length subsequences.

Resources