SAS.DCM.FORMAT_ function(input) { # input consists of scenario Price points and associated frequencies # this produces the a format for data to be transformed into # for the PHREG procedure used for DCM models # see Kuhfield (1995) pages 14-15 # no. of prices input <- input[, -1] # this gets rid of the design columns for data d <- ncol(input)/2 # no. of scenarios ns <- nrow(input) imat <- diag(1, d) out <- NULL temp <- NULL freq <- rep(0, 2 * d) for(i in 1:ns) { x <- unlist(input[i, ]) p <- x[1:d] f <- x[(1:d) + d] n <- sum(f) #print(i,p) #print(i,f) temp2 <- NULL for(j in 1:d) { freq[2 * j - 1] <- f[j] freq[2 * j] <- n - f[j] temp <- p[j] * imat[ , - j] #print(temp) temp2 <- cbind( temp2, temp ) } temp <- cbind(freq, cbind( imat, p * imat, temp2)[rep(1:d, rep( 2, d)), ]) #print(p*imat) out <- rbind(out, temp) } out <- cbind(1:(2 * d), rep(1:nrow( input), rep(2 * d, nrow( input))), 1:2, out) #labeling #lp_paste("Price",1:d,sep="") #lb_paste("Br",1:d,sep="") lb <- dimnames(input)[[2]][1:d] lp <- paste("PR", lb, sep = "_") # obtaining labels for cb1 <- outer(lb, lb, paste, sep = "_") sub1.cb <- cbind(rep(1:d, rep(d, d) ), rep(1:d, d)) sub2.cb <- - (1:d + d * (0:(d - 1) )) cb2 <- cb1[sub1.cb[sub2.cb, ]] dimnames(out) <- list(NULL, c("OBS", "SET", "T", "FREQ", lb, lp, cb2)) out } HeinekenData2008<-SAS.DCM.FORMAT(Heineken2008)