Re: [R] How to create design matrix for LLMNL?

From: Spencer Graves <spencer.graves_at_pdf.com>
Date: Fri 26 Aug 2005 - 14:29:08 EST

          I haven't seen a reply to this, so I will attempt a few comments. If you've already received an adequate reply, please excuse my tardy comments.

  1. It is generally not wise to use the name of a standard S / R function for a data.frame. Please type "df" at a command prompt to see what you get. In this case, this is probably not creating problems for you, but it might in other contexts.
  2. Did you try the examples in the help file for "createX" in library(bayesm)? If no, I suggest you do so. If yes, I suggest you examine carefully your call to "createX" in comparison with the examples and the rest of the documentation.
  3. If you still have a question, please submit another post after (re)reading posting guide! "http://www.R-project.org/posting-guide.html". You may think you already did this. However, 289 observations in an attached file is not, for me at least, a toy example within the spirit of the posting guide. That posting guide is not intended to be a burocratic obstacle. It was written to help people get quicker answers to their questions. When followed, I believe it succeeds fairly well. To me it is roughly like the famous book by George Pólya on "How to Solve It", which I also highly recommend.
	  Viel Glueck!
	  spencer graves	

Tetyana Stepanchuk wrote:

> Hello,
>
>
>
> I have a small problem with developing design matrix X, which I use in
> estimation the log-likelihood of a multinomial logit model.
>
>
>
> I have the data:
>
> number of observation - 289
>
> number of choice alternative- 3
>
> number of choice specific variables in matrix X -4
>
> matrix X =289x4
>
> I tried to use the function createX, I know that I have to get design matrix
> 289x12 (am I right?) but it always says "bad dim" (my code and data in
> attachment)
>
>
>
> Where is my fault? Can I use another method in order to create design
> matrix?
>
> Or need it at all here in logmnl (see code in attachment)?
>
>
>
> Can anyone help me with this issue?
>
>
>
> Thanks in advance,
>
> Tatyana
>
>
>
>
>
>
>
> ------------------------------------------------------------------------
>

>>df=read.table("data.dat",header=TRUE)
>>inp=as.matrix(df)

>
> Y X1 X2 X3 X4
> 1 1 1 1 65 20999.89
> 2 1 1 2 67 2719.60
> 3 1 1 3 110 3581.09
> 4 1 1 4 64 1731.63
> 5 1 1 5 84 4434.97
> 6 1 6 1 90 691.32
> 7 1 6 2 31 228.50
> 8 1 6 3 33 615.12
> 9 1 6 4 39 910.62
> 10 1 7 1 169 1246.75
> 11 1 7 2 183 1183.03
> 12 1 7 3 203 1345.32
> 13 1 7 4 177 1088.98
> 14 1 7 5 169 896.42
> 15 1 8 1 71 1264.57
> 16 1 8 2 80 1094.40
> 17 1 8 3 75 1715.99
> 18 1 8 4 55 905.37
> 19 1 8 5 67 1448.17
> 20 1 10 1 349 1396.77
> 21 1 10 2 666 2026.89
> 22 1 10 3 480 774.37
> 23 1 10 4 456 1972.15
> 24 1 11 1 500 245.88
> 25 1 11 2 288 2927.77
> 26 1 11 3 211 9221.67
> 27 1 11 4 206 5632.91
> 28 1 11 5 175 1636.62
> 29 1 12 1 107 857.06
> 30 1 12 2 87 789.25
> 31 1 12 3 103 856.27
> 32 1 12 4 377 933.74
> 33 1 12 5 229 1316.31
> 34 1 13 1 32 149.13
> 35 1 13 2 19 153.74
> 36 1 13 3 25 179.60
> 37 1 13 4 28 252.70
> 38 1 13 5 22 294.80
> 39 1 14 1 47 1261.82
> 40 1 14 2 19 2332.21
> 41 1 15 1 348 558.91
> 42 1 15 2 399 550.91
> 43 1 15 3 388 797.68
> 44 1 15 4 208 804.76
> 45 1 15 5 241 673.12
> 46 1 17 1 70 151.06
> 47 1 17 2 96 255.22
> 48 1 17 3 102 1220.30
> 49 1 17 4 128 793.54
> 50 1 18 3 10 134.95
> 51 1 18 4 28 992.30
> 52 1 21 1 85 1170.71
> 53 1 21 2 257 464.95
> 54 1 21 3 353 404.21
> 55 1 21 4 293 517.64
> 56 1 21 5 515 1202.68
> 57 1 22 1 66 372.89
> 58 1 22 2 79 498.70
> 59 1 22 3 47 304.83
> 60 1 22 4 48 430.03
> 61 1 22 5 52 319.86
> 62 1 23 1 14 165.28
> 63 1 23 2 35 2044.52
> 64 1 23 3 20 499.59
> 65 1 24 1 94 107.76
> 66 1 24 2 59 61.64
> 67 1 24 3 47 111.15
> 68 1 24 4 32 100.75
> 69 1 25 1 17 142.34
> 70 1 26 1 144 1105.71
> 71 1 26 2 196 1445.43
> 72 1 26 3 328 2297.11
> 73 1 26 4 517 2143.55
> 74 1 27 1 85 2457.58
> 75 1 27 2 99 1921.27
> 76 1 27 3 65 3380.86
> 77 1 27 4 88 2218.37
> 78 1 27 5 100 1881.00
> 79 1 29 1 107 561.27
> 80 1 29 2 67 557.43
> 81 1 29 3 49 387.71
> 82 1 30 1 77 106.50
> 83 1 30 2 225 267.87
> 84 1 30 3 520 502.18
> 85 1 30 4 552 443.07
> 86 1 30 5 319 255.50
> 87 1 31 1 38 6522.32
> 88 1 31 2 38 632.35
> 89 1 31 3 50 1615.18
> 90 1 31 4 53 1657.59
> 91 1 31 5 25 425.01
> 92 1 32 1 82 681.77
> 93 1 32 2 82 605.14
> 94 1 32 3 117 1068.86
> 95 1 32 4 90 638.95
> 96 1 33 1 53 350.89
> 97 1 33 2 39 378.53
> 98 1 33 3 44 432.31
> 99 1 34 1 61 752.13
> 100 1 34 2 76 1045.36
> 101 1 34 3 107 1344.42
> 102 1 34 4 65 1150.82
> 103 1 34 5 96 973.69
> 104 1 35 1 132 374.06
> 105 1 35 2 124 444.83
> 106 1 35 3 92 142.01
> 107 1 35 4 69 297.77
> 108 1 35 5 62 248.21
> 109 1 36 1 434 374.83
> 110 1 36 2 183 416.23
> 111 1 36 3 386 246.27
> 112 1 36 4 577 527.44
> 113 1 36 5 457 250.67
> 114 1 37 1 118 2306.72
> 115 1 37 2 169 1303.34
> 116 1 37 3 135 1741.13
> 117 1 37 4 103 1073.17
> 118 1 37 5 75 1146.11
> 119 1 40 1 66 1447.20
> 120 1 40 2 97 1352.28
> 121 1 40 3 65 1786.57
> 122 1 40 4 67 1060.59
> 123 1 42 1 26 241.23
> 124 1 42 2 43 334.35
> 125 1 42 3 65 381.51
> 126 1 42 4 9 33.14
> 127 1 43 1 39 1504.44
> 128 1 43 2 33 1144.56
> 129 1 43 3 43 870.53
> 130 1 43 4 43 969.19
> 131 1 43 5 64 1655.93
> 132 1 44 1 2 1555.55
> 133 1 45 1 22 84.39
> 134 1 46 1 46 996.07
> 135 1 46 2 33 777.97
> 136 1 46 3 60 637.64
> 137 1 46 4 42 1178.10
> 138 1 46 5 41 1054.84
> 139 1 47 1 37 1514.12
> 140 1 47 2 57 2132.21
> 141 1 47 3 53 2486.14
> 142 1 47 4 45 1807.57
> 143 1 47 5 45 1125.80
> 144 1 48 1 90 449.87
> 145 1 48 2 12 86.38
> 146 1 48 3 44 159.58
> 147 1 48 4 42 372.35
> 148 1 48 5 58 442.60
> 149 1 49 1 92 645.82
> 150 1 49 2 82 523.96
> 151 1 49 3 132 833.91
> 152 1 49 4 125 490.37
> 153 1 49 5 89 454.82
> 154 1 50 1 30 105.94
> 155 1 50 2 29 39.18
> 156 1 50 3 80 16.13
> 157 1 50 4 185 106.54
> 158 1 51 1 95 937.76
> 159 1 51 2 34 1212.81
> 160 1 51 3 42 1254.46
> 161 1 51 4 35 644.77
> 162 1 51 5 36 426.90
> 163 1 52 1 42 138.73
> 164 1 54 1 210 1841.15
> 165 1 56 1 29 191.12
> 166 1 56 2 56 640.55
> 167 1 56 3 62 562.07
> 168 1 56 4 47 290.71
> 169 1 56 5 34 314.87
> 170 1 57 1 23 478.82
> 171 1 59 1 89 812.66
> 172 1 59 2 59 797.46
> 173 1 59 3 45 769.12
> 174 1 59 4 36 609.01
> 175 1 59 5 49 734.39
> 176 1 60 1 18 162.35
> 177 1 60 2 31 273.38
> 178 1 60 3 43 293.07
> 179 1 60 4 32 532.20
> 180 1 60 5 47 343.64
> 181 1 61 1 88 1193.72
> 182 1 61 2 25 680.30
> 183 1 61 3 55 734.33
> 184 1 61 4 146 1309.36
> 185 1 61 5 130 530.16
> 186 1 62 1 66 284.50
> 187 1 62 2 30 278.39
> 188 1 62 3 26 160.81
> 189 1 64 1 234 1257.18
> 190 1 64 2 133 752.41
> 191 1 64 3 141 476.03
> 192 1 64 4 202 836.94
> 193 1 64 5 122 1979.26
> 194 1 67 1 34 153.57
> 195 1 67 2 26 83.32
> 196 1 67 3 32 238.91
> 197 1 67 4 65 348.97
> 198 1 67 5 38 199.38
> 199 1 69 1 43 266.88
> 200 1 69 2 53 1497.83
> 201 1 69 3 48 2115.32
> 202 1 69 4 46 1323.33
> 203 1 69 5 72 2097.16
> 204 1 70 1 401 87.66
> 205 1 70 2 177 80.05
> 206 1 70 3 81 105.75
> 207 1 70 4 43 50.32
> 208 1 70 5 23 55.21
> 209 3 38 1 40 17345.50
> 210 3 38 2 37 19927.04
> 211 3 38 3 42 742.45
> 212 3 53 1 181 14189.78
> 213 3 53 2 75 15132.94
> 214 3 53 3 91 14927.05
> 215 3 55 1 239 40056.22
> 216 3 55 2 798 11436.61
> 217 3 55 3 284 3031.93
> 218 3 55 4 37 1162.11
> 219 3 55 5 58 6458.99
> 220 3 65 1 41 2928.30
> 221 3 65 2 45 2447.31
> 222 3 65 3 46 2504.06
> 223 3 65 4 41 2865.30
> 224 3 65 5 41 5404.57
> 225 3 71 1 56 17897.50
> 226 2 2 1 68 2481.72
> 227 2 3 1 168 1794.23
> 228 2 3 2 164 2401.75
> 229 2 3 3 139 2229.82
> 230 2 3 4 152 2865.10
> 231 2 3 5 135 3157.92
> 232 2 4 1 37 1990.07
> 233 2 4 2 33 4441.53
> 234 2 4 3 38 2972.56
> 235 2 4 4 38 3050.71
> 236 2 4 5 27 2326.24
> 237 2 5 1 133 6481.32
> 238 2 5 2 36 2064.21
> 239 2 5 3 165 5431.46
> 240 2 5 4 131 5632.18
> 241 2 5 5 65 4805.79
> 242 2 9 1 58 295.27
> 243 2 9 2 118 4501.84
> 244 2 9 3 128 438.22
> 245 2 16 1 281 1194.92
> 246 2 16 2 227 1344.28
> 247 2 16 3 237 1027.02
> 248 2 16 4 265 1113.11
> 249 2 16 5 143 1080.23
> 250 2 18 1 34 3465.32
> 251 2 18 2 31 1879.28
> 252 2 19 1 126 1125.53
> 253 2 19 2 96 3269.87
> 254 2 20 1 42 4572.29
> 255 2 20 2 56 4020.63
> 256 2 20 3 53 94.82
> 257 2 20 4 69 2959.03
> 258 2 20 5 62 1145.52
> 259 2 28 1 106 877.37
> 260 2 28 2 139 1495.15
> 261 2 28 3 278 1170.82
> 262 2 28 4 52 3838.59
> 263 2 39 1 165 6277.17
> 264 2 39 2 117 1565.52
> 265 2 39 3 91 3096.30
> 266 2 39 4 93 2038.49
> 267 2 41 1 151 3657.07
> 268 2 41 2 169 4371.29
> 269 2 41 3 171 3543.19
> 270 2 41 4 82 2762.35
> 271 2 41 5 59 5054.83
> 272 2 58 1 96 6062.83
> 273 2 58 2 53 3730.32
> 274 2 58 3 24 1044.85
> 275 2 58 4 4 1000.44
> 276 2 58 5 0 1144.44
> 277 2 63 1 130 145.73
> 278 2 63 2 82 264.27
> 279 2 63 3 115 219.01
> 280 2 63 4 158 199.87
> 281 2 63 5 115 286.83
> 282 2 66 1 218 7964.96
> 283 2 66 2 198 4512.50
> 284 2 66 3 169 4954.49
> 285 2 68 1 3025 1494.90
> 286 2 68 2 3333 1355.09
> 287 2 68 3 3969 1848.35
> 288 2 68 4 3059 1506.72
> 289 2 68 5 4557 2339.48
>
>>y=as.numeric(inp[,1])
>>Xa=matrix(inp[,2:5],byrow=TRUE,ncol=3)

>
> Xa=cbind(Xa,-Xa)
>
>>X=createX(p=nsize,na=nxvar,Xa=Xa,nd=NULL,Xd=NULL,INT=TRUE)

>
> Fehler: bad Xa dim, dim= 386bad Xa dim, dim= 6
>
>
>
>
>
> ------------------------------------------------------------------------
>
> logmnl<- function(X,y,nsize,nxvar,nhh,beta){
> #Function evaluates the log-likelihood of multinimial logit model
> #X is of dimension X(nsize,nxvar,nhh) and y(nsize,nhh)
> # nsize=number of alternatives
> # nxvar=number of x variables
> # nhh=number of observations
> df=read.table("TS_3part.dat",header=TRUE)
> inp=as.matrix(df)
> y=as.numeric(inp[,1])
> + nsize=3
> + nxvar=4
> + nhh=length(y)
> X=createX(p=p,na=1,Xa=data[,3:8],nd=NULL,Xd=NULL,INT=TRUE,base=1)
> + xp<-array(0,dim=c(nsize))
> + logprob<-0
> + for(i in 1:nhh)
> + {
> + for(j in 1:nsize)
> + {
> + xp[j]<-exp(t(X[j,,i]) %*% beta)
> + }
> + denom=sum(xp)
> + for(j in 1:nsize)
> + {
> + if(y[j,i]==1) prob=xp[j]/denom
> + }
> + logprob=logprob+log(prob)
> + }
> + logprob
> + }
>
>
> ------------------------------------------------------------------------
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
-- 
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

spencer.graves@pdf.com
www.pdf.com <http://www.pdf.com>
Tel:  408-938-4420
Fax: 408-280-7915

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri Aug 26 14:36:16 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:57:48 EST