#> library ( AlgDesign )
#> ffd = gen.factorial ( c ( 2, 2, 4 ), varNames = c ( 'HAC', 'ECO', 'PRI' ), factors = 'all' )
#> set.seed(54321)
#> des = optFederov ( ~., ffd, 8 )
#> alt1 = des$design
#> alt2 = alt1
#> alt1 = transform ( alt1, r1 = runif ( 8 ) )
#> alt2 = transform ( alt2, r2 = runif ( 8 ) )
#> alt1.sort = alt1 [ order ( alt1$r1 ), ]
#> alt2.sort = alt2 [ order ( alt2$r2 ), ] 
#> library ( survival )
#> source ('MktRoll.R')
#> results = MktRoll ( 10, 8, 3, data, labs[,1:3] )
#> clogout1 = clogit ( RES~ASC+HAC+ECO+PRI+strata(STR), data=results )
#> clogout2 = clogit ( RES~ASC+HAC+ECO+PRI+PRI:FEM+strata(STR), data=results )

labels = matrix ( c (
  "No" , "No" , "145 yen" , "Yes" ,"No"  ,"160 yen",
  "Yes", "No" , "155 yen" , "Yes" ,"Yes" ,"145 yen",
  "Yes", "Yes", "150 yen" , "No"  ,"Yes" ,"160 yen",
  "No" , "No" , "150 yen" , "No"  ,"No"  ,"145 yen",
  "No" , "Yes", "155 yen" , "Yes" ,"Yes" ,"150 yen",
  "Yes", "Yes", "145 yen" , "Yes" ,"No"  ,"155 yen",
  "No" , "Yes", "160 yen" , "No"  ,"Yes" ,"155 yen",
  "Yes", "No" , "160 yen" , "No"  ,"No"  ,"150 yen"),
  ncol = 6, byrow=T )

labs = matrix ( c (
  0, 0, 145, 1, 0, 160,
  1, 0, 155, 1, 1, 145,
  1, 1, 150, 0, 1, 160,
  0, 0, 150, 0, 0, 145,
  0, 1, 155, 1, 1, 150,
  1, 1, 145, 1, 0, 155,
  0, 1, 160, 0, 1, 155,
  1, 0, 160, 0, 0, 150),
  ncol = 6, byrow = T )
colnames ( labs ) = c ( rep ( c( 'HAC', 'ECO', 'PRI'), 2 ) )

data = matrix ( c (
  3,   2,    2,    1,    3,    3,    1,    3,  1,
  3,   2,    1,    3,    2,    1,    3,    1,  0,
  1,   2,    3,    1,    1,    1,    2,    1,  1,
  3,   3,    3,    1,    2,    1,    3,    3,  0,
  3,   2,    1,    1,    3,    1,    3,    3,  1,
  1,   3,    3,    3,    3,    1,    3,    3,  0,
  3,   3,    3,    1,    2,    1,    3,    3,  1,
  2,   2,    3,    2,    2,    3,    3,    1,  0,
  3,   2,    2,    2,    2,    1,    3,    2,  1,
  3,   1,    3,    3,    2,    2,    3,    3,  0),
  ncol = 9, byrow = T )

n   = nrow ( data )
q   = nrow ( labs )
a   = 3
key = labs

MktRoll = function ( n, q, a, data, key )
  {
    # n    = number of respondents
    # q    = number of questions (choice sets)
    # a    = size of choice set
    # data = responses: n x q
    # key  = ???

    debug = FALSE
    z = n * q * a
    c = 1 + 2 + dim ( key )[2] + 1
    r = matrix ( rep ( 0, z * c ), ncol = c, byrow = T )
    if ( debug )
      {
        print ( paste ( 'Result is a ', nrow(r), 'by', ncol(r), 'matrix' ) )
      }
    cnames = c ( 'STR', 'RES', 'ASC', colnames ( key ), 'FEM' )
    colnames ( r ) = cnames
    
    l = 1
    for ( i in 1 : n ) # for each respondent
      {
        for ( j in 1 : q ) # for each question
          {
            for ( k in 1 : a ) # for each alternative
              {
                r [ l, 1 ] = 100 * i + j  # Stratification variable
                if ( data [ i, j ] == k ) { r [ l, 2] = 1 } # RES (result, i.e. chosen alternative)
                if ( k == 1 )
                  {
                    r [ l, 3] = 1 # ASC (alternative-specific constant)
                    r [ l, 4:6 ] = labs [ j, 1:3 ] # values of factors for this alternative
                  }
                if ( k == 2 )
                  {
                    r [ l, 3] = 1 # ASC (alternative-specific constant)
                    r [ l, 4:6 ] = labs [ j, 4:6 ]
                  }
                dem = data [ i, q + 1 ]
                if ( debug )
                  {
                    print ( paste ("inserting", dem, "for respondent", i, "in row", l ) )
                  }
                r [ l, 7 ] =  dem # Demographic variable
                l = l + 1
              }
          }
      }
    return ( data.frame ( r ) )
  }

