"initial values are not feasible" error message while running structural equation model in Stata? - stata

I am working on a structural equation model (sem) model with 47 observed variables and 6 latent variables, of which 5 observed variables are endogenous and one latent variable is endogenous. Data has no missing values and sample size is 4,634.
I ran sem in Stata using the following command:
sem (I -> i1 i2 i3 i4 i5_1) ///
(N -> n1 n2 n3 n4) ///
(S -> s1 s2 s3 s4 s5 s6 s7 s8 s9) ///
(T -> t1 t2 t3 t4) ///
(SES -> se1 se2 se3 se4 se5 se6 se7 se8 se9 se10 ///
se11 se12 se13 se14 se15 se16 se17 se18 se19 se20) ///
(CS -> c1 c2 c3 c4 c5) ///
(CS <- I N S T SES)
It returned the following error message:
initial values are not feasible
Why am I receiving this message? How can I deal with this error?

I would start by looking at each measurement model separately, and see if there are problems there. i.e.:
sem ( i1 i2 i3 i4 i5_1 <- I)
sem ( i1 i2 i3 i4 i5_1 <- N)
etc.
My guess would be that the model for SES might prove to be the problem.
Edit:
Based on your comment we now know that the measurement models in isolation converge. Next step would be to check each of the measurement models to see whether they make sense: Do each of the loading have the expected sign? Are there loading that are unexpectedly large or small? If you see that, you need to figure out why that is the case. This just requires staring at your data, looking at graphs and correlation tables.
If there is no problem with your measurement model, then the next step would be to look at the structural part. Obviously you cannot do the same trick as with the measurement model, that is, you cannot estimate the structural part without the measurement models. The structural contains latent variables, and it is the measurement models that define what they are. So without measurement models, the structural model is not identified.
What I would do instead is simplify your model, and than add complication till you run into problems. For example I might start with:
sem (I -> i1 i2 i3 i4 i5_1) ///
(CS -> c1 c2 c3 c4 c5) ///
(CS <- I)
Than continue with:
sem (I -> i1 i2 i3 i4 i5_1) ///
(N -> n1 n2 n3 n4) ///
(CS -> c1 c2 c3 c4 c5) ///
(CS <- I N)
etc.
That way you can find which latent variable causes trouble. My first move would be to look at the measurement model of that variable and look at the scale of that variable. By default sem "borrows" the scale of one of the observed variables by setting the loading for that variable to 1. Is that variable in some sense "weird"? Similarly I would also look at the scale for your endogenous latent variable CS. If they are weird, you can choose to constrain the loading of another variable with a more reasonable scale to 1, or you can "standardize" your latent variable by constraining the variance of the latent variable to be 1.

Related

How to create an alphanumeric grid in a certain sequence allowing double digit numbers?

I have a grid feature class that varies in size and shape. My test shapefile is a 3x4 grid. I need to create an alphanumeric sequence that goes in a specific order but can be scaled for a different size grid. Below is the order the grid is in:
A4 | B4 | C4
A3 | B3 | C3
A2 | B2 | C2
A1 | B1 | C1
and to use this alphanumeric sequence, the list will need to be printed in a specific order (starting from the bottom left of the table, moving to the right, and then returning to the left value on the next row up:
A1, B1, C1, A2, B2, C2, A3, B3, C3, A4, B4, C4
I had this:
from itertools import product
from string import ascii_uppercase, digits
for x, y in product(ascii_uppercase, digits):
print('{}{}'.format(x, y))
It generates a sequence like: A0 through A9, then B0 through B9, and so forth.
However I also need larger grids so the script would have to compensate and allow the sequence to use double digits after 9 if the grid is larger than 9 high.
ie. A10, B10, C10
I then tried to make 2 lists and then combine them together, but I ran into the problem of joining these in the sequence I need.
w = 3
h = 4
alpha = []
numeric = []
for letter in ascii_uppercase[:w]:
alpha.append(letter)
for num in range(1, h+1):
numeric.append(num)
I assume I might not need to make a numeric list, but don't know how to do it. I know slightly more than just the basics of python and have created so more complex scripts, but this is really puzzling for me! I feel like I am so close but missing something really simple from both of my samples above. Thank you for any help you can give me!
Solved, here is what I have for others who might need to use my question:
w = 9
h = 20
alpha = []
numeric = []
for letter in ascii_uppercase[:w]:
alpha.append(letter)
for num in range(1, h+1):
numeric.append(num)
longest_num = len(str(max(numeric)))
for y in numeric:
for x in alpha:
print '{}{:0{}}'.format(x, y, longest_num)
I didn't need the code formatted as a table since I was going to perform a field calculation in ArcMap.
After you compute numeric, also do:
longest_num = len(str(max(numeric)))
and change your format statement to:
'{}{:0{}}'.format(x, y, longest_num)
This ensures that when you get to double digits you get the following result:
A12 | B12 | C12
A11 | B11 | C11
...
A02 | B02 | C02
A01 | B01 | C01
To actually print the grid however you need to change your code:
longest_num = len(str(max(numeric)))
for y in reversed(numeric):
print(" | ".join('{}{:0{}}'.format(x, y, longest_num)
for x in alpha))

Minimum between two boolean decision variables in Pyomo

I need to do something like this:
d1 == min(d2,d3)
where d is a decision variable. I need to use Pyomo. In cplex the solution is achieved by the funnction minl, how can do this in Pyomo or in an equivalent linear form?
I searched for a solution on Google and found that I could assert that d1 must be less or equal to d2 and d3. But this do not fit my proble, because if d2 and d3 is equal to 1, d1 <= 1 while I need d1 == 1.
Thanks for replies.
When the d variables are binary variables,
d1 = min(d2,d3)
is really the same as multiplication
d1 = d2*d3
This is often linearized as
d1 <= d2
d1 <= d3
d1 >= d2+d3-1

Predict data values given history and constraints

If I have data series and a set of constraints and want to predict the most likely values, what is the right algorithm or approach? For example, given the data table as follows:
The first three rows illustrate typical data values. Imagine we have dozens or hundreds of such rows. The constraints on the system are as follows:
G1 + G2 + G3 + G4 == D1 + D2 + D3
G1 + G2 = D1 - C1
G3 = D2 + C1 - C2
G4 = D3 + C2
So, given D1, D2 and D3 we need to predict G1, G2, G3, G4, C1, and C2. Note that there may not necessarily be enough information to solve the system by linear programming alone and so some kind of trend analysis or probability distribution might need to be made.
What is the right algorithm or approach to solve a problem like this?

Simplifying equations in Sympy using inequalities

I am trying in Sympy to use a standard engineering method of simplifying an equation, where you know one variable is much larger or smaller than another. For instance, given the equation
C1*R2*s+C1*R2+R1+R2
and knowing that
R1 >> R2
the equation can be simplified to
C1*R2*s+C1*R2+R1.
What you'd normally do by hand is divide by R1, giving
C1*R2*s/R1+C1*R2/R1+1+R2/R1
then anyplace you see R2/R1 by itself you can set it to zero and then multiply by R1. I've not been able to figure how this would be done in Sympy. Obviously it's easy to do the division step, but I haven't been able to figure out how to do the search and replace step- just using subs gives you
R1
which isn't the right answer. factor, expand, collect, don't seem to get me anywhere.
Using replace instead of subs works here.
C1, C2, R1, R2 = sp.symbols('C1, C2, R1, R2', real = True)
s = sp.symbols('s')
expr = C1*R2*s+C1*R2+R1+R2
print('original expression:', expr)
expr_approx = (R1 * ((expr/R1).expand().replace(R2/R1,0))).simplify()
print('approximate expression:', expr_approx)
original expression: C1*R2*s + C1*R2 + R1 + R2
approximate expression: C1*R2*s + C1*R2 + R1

Sum of product: can we vectorize the following in C++? (using Eigen or other libraries)

UPDATE: the (sparse) three-dimensional matrix v in my question below is symmetric: v(i1,i2,i3) = v(j1,j2,j3) where (j1,j2,j3) is any of the 6 permutations of (i1,i2,i3), i.e.
v(i1,i2,i3) = v(i1,i3,i2) = v(i2,i3,i1) = v(i2,i1,i3) = v(i3,i1,i2) = v(i3,i2,i1).
Moreover, v(i1,i2,i3) != 0 only when i1 != i2 && i1 != i3 && i2 != i3.
E.g. v(i,i,j) = 0, v(i, k, k) = 0, v(k, j, k) = 0, etc...
I thought that with this additional information, I could already get a significant speed-up by doing the following:
Remark: v contains duplicate values (a triplet (i,j,k) has 6 permutations, and the values of v for these 6 are the same).
So I defined a more compact matrix uthat contains only non-duplicates of v. The indices of u are (i1,i2,i3) where i1 < i2 < i3. The length of u is equal to the length of v divided by 6.
I computed the sum by iterating over the new value vector and the new index vectors.
With this, I only got a little speed-up. I realized that instead of iterating N times doing a multiplication each time, I iterated N/6 times doing 6 multiplications each time, and that's pretty much the same as before :(
Hope somebody could come up with a better solution.
--- (Original question) ---
In my program I have an expensive operation that is repeated every iteration.
I have three n-dimensional vectors x1, x2 and x3 that are supposed to change every iteration.
I have four N-dimensional vectors I1, I2, I3 and v that are pre-defined and will not change, where:
I1, I2 and I3 contain the indices of respectively x1, x2 and x3 (the elements in I_i are between 0 and n-1)
v is a vector of values.
For example:
We can see v as a (reshaped) sparse three-dimensional matrix, each index k of v corresponds to a triplet (i1,i2,i3) of indices of x1, x2, x3.
I want to compute at each iteration three n-dimensional vectors y1, y2 and y3 defined by:
y1[i1] = sum_{i2,i3} v(i1,i2,i3)*x2(i2)*x3(i3)
y2[i2] = sum_{i1,i3} v(i1,i2,i3)*x1(i1)*x3(i3)
y3[i3] = sum_{i1,i2} v(i1,i2,i3)*x1(i1)*x2(i2)
More precisely what the program does is:
Repeat:
Compute y1 then update x1 = f(y1)
Compute y2 then update x2 = f(y2)
Compute y3 then update x3 = f(y3)
where f is some external function.
I would like to know if there is a C++ library that helps me to do so as fast as possible. Using for loops is just too slow.
Thank you very much for your help!
Update: Looks like it's not easy to get a better solution than the straight-forward for loops. If the vector of indices I1 above is ordered in non-decreasing order, can we compute y1 faster?
For example: I1 = [0 0 0 0 1 1 2 2 2 3 3 3 ... n n].
The simple answer is no, at least, not trivially. Your access pattern (e.g. x2(i2)*x3(i3)) does not (at least at compile time) access contiguous memory, but rather has a layer of indirection. Due to this, SIMD instructions are pretty useless, as they work on chunks of memory. What you may want to consider doing is creating a copy of xM sorted according to iM, removing the layer of indirection. This should reduce the number of cache misses in that xM(iM) generates and since it's accessed N times, that may reduce some of the wall time (assuming N is large).
If maximal accuracy is not critical, you may want to consider using a FFT method instead of the convolution (at least, that's how I understood your question. Feel free to correct me if I'm wrong).
Assuming you are doing a convolution and the vectors (a and b, same size as in your question) are large, the result (c) can be calculated naïvely as
// O(n^2)
for(int i = 0; i < c.size(); i++)
c(i) = a(i) * b.array();
Using the convolution theorem, you could take the Fourier transform of both a and b and perform an element wise multiplication and then take the inverse Fourier transform of the result to get c (will probably differ a little):
// O(n log(n)); note A, B, and C are vectors of complex floating point numbers
fft.fwd(a, A);
fft.fwd(b, B);
C = A.array() * B.array();
fft.inv(C, c);