☑ This topic is solved.
Need further help from the community? Please
sign in and ask a new question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Posted 06-07-2023 04:26 AM
(1201 views)
I have the following code for a multivariate GMM. When I run it, it gives an error. Any advice or guidance would be appreciated.
proc iml; NOTE: IML Ready 80 use WORK.IMPORT4; 81 read all var {X1 X2} into x; 81 ! /* Read multiple variables x1 and x2 */ 82 83 mu = {120 150}; 83 ! /* Mean vector */ 84 si = {20 20}; 84 ! /* Standard deviation vector */ 85 pi = {0.5 0.5}; 85 ! /* Mixing weights */ 86 87 nVars = ncol(x); 87 ! /* Number of variables */ 88 nClusters = nrow(mu); 88 ! /* Number of clusters */ 89 90 /* Estep */ 91 stop = 0; 92 results = j(1, 1 + nClusters + nClusters*nVars + nClusters*nVars, .); 93 idx = 1; 94 do i = 1 to 200 while (stop = 0); 95 print "Iteration" i; 96 97 /* Compute gamma */ 98 gamma = pi[1] * pdf('normal', x, mu[1], si[1]) + 99 pi[2] * pdf('normal', x, mu[2], si[2]); 100 gamma = gamma / (gamma[,1] + gamma[,2]); 100 ! /* Normalize gamma */ 101 102 /* Check for convergence */ 103 if i = 1 then print gamma; 104 105 nk = gamma[,1] + gamma[,2]; 105 ! /* Calculate nk */ 106 107 npi = nk / sum(nk); 107 ! /* Calculate npi */ 108 109 nmu = j(1, nVars, 0); 110 do k = 1 to nClusters; 111 nmu = nmu + (gamma[,k] # x[,1:nVars]); 112 end; 113 nmu = nmu / nk; 113 ! /* Calculate nmu */ 114 115 nsi2 = j(nVars, nVars, 0); 116 116 ! do k = 1 to nClusters; 117 117 ! diff = x[,1:nVars] - nmu; 118 118 ! nsi2 = nsi2 + (gamma[,k] * (diff` * diff)); 119 119 ! end; 120 121 nsi = sqrt(nsi2); 121 ! /* Calculate nsi */ 122 123 diff = max(abs(mu - nmu)); 123 ! /* Calculate diff */ 124 125 if diff < 0.005 then stop = 1; 126 127 mu = nmu; 127 ! /* Update mu */ 128 si = nsi; 128 ! /* Update si */ 129 pi = npi; 129 ! /* Update pi */ 130 131 results[idx, 1] = i; 132 results[idx, 2:(1+nClusters)] = npi; 133 results[idx, (2+nClusters):(1+nClusters+nVars)] = nmu; 134 results[idx, (2+nClusters+nVars):(1+nClusters+2*nVars)] = nsi; 135 idx = idx + 1; 136 end; ERROR: (execution) Matrices do not conform to the operation. operation : * at line 118 column 31 operands : _TEM1001, _TEM1003 _TEM1001 1500 rows 1 col (numeric) _TEM1003 2 rows 2 cols (numeric) 5054876.6 5443147.7 5443147.7 6657773.5 statement : ASSIGN at line 118 column 6 137 138 create results from results; 139 append from results; 140 close results; NOTE: The data set WORK.RESULTS has 1 observations and 6 variables. 141 142 quit;
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Are you trying to fit a Gaussian mixture model to data by using the EM algorithm? If so, try
nClusters = NCOL(mu);
also, see the IML program and discussion at
https://blogs.sas.com/content/iml/2020/07/23/fit-multivariate-gaussian-mixture-em-algorithm.html
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Please have a look the blogpost from @Rick_SAS : https://blogs.sas.com/content/iml/2013/12/09/matrices-do-not-conform-to-the-operation.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Why not post it at IML forum ? @Rick_SAS is there.
https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/bd-p/sas_iml
https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/bd-p/sas_iml
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Are you trying to fit a Gaussian mixture model to data by using the EM algorithm? If so, try
nClusters = NCOL(mu);
also, see the IML program and discussion at
https://blogs.sas.com/content/iml/2020/07/23/fit-multivariate-gaussian-mixture-em-algorithm.html