Skip to content
Prev 369608 / 398503 Next

New var

Since the number of choices is small (6), how about this?

Starting with Jeff's initial DFM:

DFM <- structure(list(obs = 1:6, start = structure(c(16467, 14710, 13152, 
13787, 15126, 12696), class = "Date"), end = structure(c(17167, 
14975, 13636, 13879, 15340, 12753), class = "Date"), D = c(700, 
265, 484, 92, 214, 57), bin = structure(c(6L, 3L, 5L, 1L, 3L, 
1L), .Label = c("[0,100)", "[100,200)", "[200,300)", "[300,400)", 
"[400,500)", "[500,Inf)"), class = c("ordered", "factor"))), .Names = c("obs", 
"start", "end", "D", "bin"), row.names = c(NA, -6L), class = "data.frame")

Construct a matrix of the six alternatives:

tvals <- c(1, -1, -1, -1, -1, 0, 1, -1, -1, -1, 0, 0, 1, -1, -1, 0, 0, 
    0, 1, -1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0)
tmat <- matrix(tvals, 6, 5, byrow=TRUE)
colnames(tmat) <- paste0("t", 1:5)
tmat
#      t1 t2 t3 t4 t5
# [1,]  1 -1 -1 -1 -1
# [2,]  0  1 -1 -1 -1
# [3,]  0  0  1 -1 -1
# [4,]  0  0  0  1 -1
# [5,]  0  0  0  0  1
# [6,]  0  0  0  0  0

idx <-as.numeric(DFM$bin)
(DFM <- data.frame(DFM, tmat[idx, ]))
#    obs      start        end   D       bin t1 t2 t3 t4 t5
# 1   1 2015-02-01 2017-01-01 700 [500,Inf)  0  0  0  0  0
# 2   2 2010-04-11 2011-01-01 265 [200,300)  0  0  1 -1 -1
# 3   3 2006-01-04 2007-05-03 484 [400,500)  0  0  0  0  1
# 4   4 2007-10-01 2008-01-01  92   [0,100)  1 -1 -1 -1 -1
# 5   5 2011-06-01 2012-01-01 214 [200,300)  0  0  1 -1 -1
# 6   6 2004-10-05 2004-12-01  57   [0,100)  1 -1 -1 -1 -1


David L. Carlson
Department of Anthropology
Texas A&M University

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Val
Sent: Sunday, June 4, 2017 11:31 AM
To: Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
Cc: r-help at R-project.org
Subject: Re: [R] New var

Thank you Jeff and All,

Within a given time period (say 700 days, from the start day),  I am
expecting measurements taken at each time interval;. In this case "0" means
measurement taken, "1"  not taken (stopped or opted out  and " -1"  don't
consider that time period for that individual. This will be compared with
the actual measurements taken (Observed- expected)  within each time
interval.




On Sat, Jun 3, 2017 at 9:50 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote:
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.