How do I make the canonical PROC CALIS LINEQS example work? - sas

I'm trying out the PROC CALIS LINEQS example outlined here (it works when I use the PATH and RAM examples) using the Wheaton dataset (I've renamed the headers to match the code below) with this code:
proc calis nobs=932 data=Wheaton;
lineqs
Anomie67 = 1.0 * f_Alien67 + E1,
Powerless67 = 0.833 * f_Alien67 + E2,
Anomie71 = 1.0 * f_Alien71 + E3,
Powerless71 = 0.833 * f_Alien71 + E4,
Education = 1.0 * f_SES + E5,
SEI = lambda * f_SES + E6,
f_Alien67 = gamma1 * f_SES + D1,
f_Alien71 = gamma2 * f_SES + beta * Alien67 + D2;
std
E1 = theta1,
E2 = theta2,
E3 = theta1,
E4 = theta2,
E5 = theta3,
E6 = theta4,
D1 = psi1,
D2 = psi2,
f_SES = phi;
cov
E1 E3 = theta5,
E2 E4 = theta5;
run;
but I get this error:
"Predictor variable Alien67 in the equation with outcome variable f_Alien71 is neither a manifest, an F, an E, nor a D variable."
What am I doing wrong?

okay, I found the error - I had to consult page 450 of the SAS OnlineDoc™: Version 8 to find the solution which is basically to change this line of code:
f_Alien71 = gamma2 * f_SES + beta * Alien67 + D2;
to
f_Alien71 = gamma2 * f_SES + beta * f_Alien67 + D2;
I got a clue when I was reading page 450 because V5 in the book which corresponds to SEI in the code was using F3 (which was f_SES) as an input and then I noticed that the input to F2 in the book (which was f_Alien71 in the code) was F1 (which was f_Alien67 in the code) and I found that there was a mismatch.

Related

converting DFA to regular expression using Transitive closure method

The following example shows a simple DFA with one accepting state q2 :
Based on the R(i,j,k) Algorithm shown above i want to convert this DFA to regular expression, unfortunately i can't find a good definition of the K. My question is what does the K mean ?
is it the number of states (in this case 3) or something else?
Then, we solve those equations to get the equation for qi in terms of α ij and that expression is the required solution, where qi is a final state. It is shown below −
q 1= q a + q3a + € ( € move is because q1is the initial state)
q2 = q1b + q2b + q3b
q3 = q2a
Now, we will solve these equations, as shown below−
. q2 = q1b + q2b + q3b
= q1b + q2b + (q2a)b (Substituting value of q3)
= q1b + q2(b + ab)
= q1b (b + ab) * (Applying Arden's Theorem)
. q1 = q1a + q3a + €
= q1a + q2aa + € (Substituting value of q3)
= q1a + q1b(b + ab*)aa + € (Substituting value of q2)
= q1(a + b(b + ab) *aa) + €
= € (a+ b(b + ab) *aa) *
= (a + b(b + ab) *aa) *
Hence, the regular expression is (a + b(b + ab) *aa) *.

Nested IF AND OR in Excel

How do I write the formula for the following:
if B2 = "SF" and D2 = "1"
then H2 = E2 + .75
else if B2 = "SF" and D2 = ".25"
then H2 = E2 + .625
else if B2 = "CW" and D2 = "1"
then H2 = E2 + 1
I want my answers to be in H2, with data being entered into B2, D2 and E2.
=if(AND(B2="SF",D2=1)=TRUE,E2+0.75,if(AND(B2="SF",D2=0.25)=TRUE,E2+0.625,if(AND(B2="CW",D2=1)=TRUE,E2+1,0)))
Regards

Reading csv with several subgroups

I have a csv-file that contains "pivot-like" data that I would like to store into a pandas DataFrame. The original data file is divided using different number of whitespaces to differentiate between the level in the pivot-data like so:
Text that I do not want to include,,
,Text that I do not want to include,Text that I do not want to include
,header A,header B
Total,100,100
A,,2.15
a1,,2.15
B,,0.22
b1,,0.22
" slightly longer name"...,,0.22
b3,,0.22
C,71.08,91.01
c1,57.34,73.31
c2,5.34,6.76
c3,1.33,1.67
x1,0.26,0.33
x2,0.26,0.34
x3,0.48,0.58
x4,0.33,0.42
c4,3.52,4.33
x5,0.27,0.35
x6,0.21,0.27
x7,0.49,0.56
x8,0.44,0.47
x9,0.15,0.19
x10,,0.11
x11,0.18,0.23
x12,0.18,0.23
x13,0.67,0.85
x14,0.24,0.2
x15,0.68,0.87
c5,0.48,0.76
x16,,0.15
x17,0.3,0.38
x18,0.18,0.23
d2,6.75,8.68
d3,0.81,1.06
x19,0.3,0.38
x20,0.51,0.68
Others,24.23,0
N/A,,
"Text that I do not want to include(""at all"") ",,
(It looks aweful, but you should be able to paste in e.g. Notepad to see it a bit clearer)
Basically, there are only two columns a and b, but the rows are indented using 0, 3, 6, 9, ... etc whitespaces to differentiate between the levels. So for instance,
zero level, the main group, A has 0 spaces,
first level a1 has 3 spaces,
second level a2 has 6 spaces,
third level a3 has 9 spaces and
fourth and final level has 12 spaces with the corresponding values for columns a and b respectively.
I would now like to be able to read and group this data on these levels in order to create a new summarizing DataFrame, with columns corresponding to these different levels, looking like:
Level 4 Diff(a,b) Level 0 Level 1 Level 2 Level 3
x7 525 C c1 c2 c3
x5 -0.03 A a1 a22 NaN
x4 -0.04 A a1 a22 NaN
x8 -0.08 C c1 c2 c3
…
Any clue on how to do this?
Thanks
Easiest is to split this into different functions
read the file
parse the lines
generate the 'tree'
construct the DataFrame
Parse the lines
def parse_file(file):
import ast
import re
pat = re.compile(r'^( *)(\w+),([\d.]+),([\d.]+)$')
for line in file:
r = pat.match(line)
if r:
spaces, label, a, b = r.groups()
diff = ast.literal_eval(a) - ast.literal_eval(b)
yield len(spaces)//3, label, diff
Reads each line, yields the level, 'label' and diff using a regular expression. I use ast to convert the string to int or float
Generate the tree
def parse_lines(lines):
previous_label = list(range(5))
for level, label, diff in lines:
previous_label[level] = label
if level == 4:
yield tuple(previous_label), diff
Initiates a list of length 5, and then overwrites the level this node is on.
Construct the DataFrame
with StringIO(file_content) as file:
lines = parse_file(file)
index, data = zip(*parse_lines(lines))
idx = pd.MultiIndex.from_tuples(index, names=[f'level_{i}' for i in range(len(index[0]))])
df = pd.DataFrame(data={'Diff(a,b)': list(data)}, index=idx)
Opens the file, constructs the index and generates the DataFrame with the different levels in the index. If you don't want this, you can add a .reset_index() or construct the DataFrame slightly different
df
level_0 level_1 level_2 level_3 level_4 Diff(a,b)
A a1 a2 a3 x1 -0.07
A a1 a2 a3 x2 -0.08000000000000002
A a1 a22 a3 x3 -0.04999999999999999
A a1 a22 a3 x4 -0.04000000000000001
A a1 a22 a3 x5 -0.03
A a1 a22 a3 x6 -0.06999999999999998
C c1 c2 c3 x7 525.0
C c1 c2 c3 x8 -0.08000000000000002
alternative for missing levels
def parse_lines(lines):
labels = [None] * 5
previous_level = None
for level, label, diff in lines:
labels[level] = label
if level == 4:
if previous_level < 3:
labels = labels[:previous_level + 1] + [None] * (5 - previous_level)
labels[level] = label
yield tuple(labels), diff
previous_level = level
the items under a22 don't seem to have a level_3, so it copies that from the previous. If this is unwanted, you can take this variation
df
level_0 level_1 level_2 level_3 level_4 Diff(a,b)
C c1 c2 c3 x1 -0.07
C c1 c2 c3 x2 -0.08000000000000002
C c1 c2 c3 x3 -0.09999999999999998
C c1 c2 c3 x4 -0.08999999999999997
C c1 c2 c4 x5 -0.07999999999999996
C c1 c2 c4 x6 -0.060000000000000026
C c1 c2 c4 x7 -0.07000000000000006
C c1 c2 c4 x8 -0.02999999999999997
C c1 c2 c4 x9 -0.04000000000000001
C c1 c2 c4 x11 -0.05000000000000002
C c1 c2 c4 x12 -0.05000000000000002
C c1 c2 c4 x13 -0.17999999999999994
C c1 c2 c4 x14 0.03999999999999998
C c1 c2 c4 x15 -0.18999999999999995
C c1 c2 c5 x17 -0.08000000000000002
C c1 c2 c5 x18 -0.05000000000000002
C c1 d2 d3 x19 -0.08000000000000002
C c1 d2 d3 x20 -0.17000000000000004

Calculating distance and speed using gps coordinates using qt c++

I am attempting to convert the first code snippet at:
Calculate distance between 2 GPS coordinates
// var R = 6371; // km
// var dLat = (lat2-lat1).toRad();
// var dLon = (lon2-lon1).toRad();
// var lat1 = lat1.toRad();
// var lat2 = lat2.toRad();
// var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
// Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
// var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
// var d = R * c;
(also shown below) for use in qt c++. It is used in a program that simulates gps flight inside of a radio control simulator. The GpsCalcLoop grabs gps latitude and longitude (in decimal format) once per second. These are fed into my converted code in hopes of getting a distance traveled during that one second of time. Speed would then be calculated.
I used the information at this page:
http://doc.qt.io/qt-4.8/qtcore-qmath-h.html
to convert the math functions. The output, however, is not what I expected. No matter what is input into lat2, lat1, lon2, and lon1 the results are always the same, '16777200.00'.
I am hoping that someone more familiar with qt c++ will be able to show me where I have gone wrong with the conversion. or point me to where I can find qt c++ code that is working.
I will add that I have attempted to use "#include "<"QGeoPositionInfo">" but get a 'No such file or directory' error when running the program.
void TelemetrySimulator::onGpsCalcLoop()
{
if(rungps)
{
ui -> valuetest1 -> setValue(flat);
ui -> valuetest2 -> setValue(flat2);
ui -> valuetest3 -> setValue(flon);
ui -> valuetest4 -> setValue(flon2);
ui -> valuetest5 -> setValue(flat-flat2);
ui -> valuetest6 -> setValue(flon-flon2);
flat2 = flat;
flon2 = flon;
//--https://stackoverflow.com/questions/365826 //calculate-distance-between-2-gps-coordinates--
// var R = 6371; // km
// var dLat = (lat2-lat1).toRad();
// var dLon = (lon2-lon1).toRad();
// var lat1 = lat1.toRad();
// var lat2 = lat2.toRad();
// var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
// Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
// var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
// var d = R * c;
//--------------my conversio to qt c++---------
//---converted to qt using this web page -> http://doc.qt.io/qt-4.8/qtcore-qmath-h.html----
double R, dLat, dLon, lat1, lat2, lon2, lon1, a, c, d;
lat2 = flat2;
lat1 = flat;
lon2 = flon2;
lon1 = flon;
R = 6371; // km
dLat = qAcos(lat2-lat1); //dLat = (lat2-lat1).toRad(); instead of qAcos could be qAsin, qAtan, qCos, qSin,
// qTan. All refer to Radials.
dLon = qAcos(lon2-lon1); //dLon = (lon2-lon1).toRad();
lat1 = qAcos(lat1); //lat1 = lat1.toRad();
lat2 = qAcos(lat2); //lat2 = lat2.toRad();
a = qSin(dLat/2) * qSin(dLat/2) + //Math.sin(dLat/2) * Math.sin(dLat/2) +
qSin(dLon/2) * qSin(dLon/2) * qCos(lat1) * qCos(lat2);//Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
c = 2 * qAtan2(qSqrt(a), qSqrt(1-a)); //Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
d = R * c;
ui -> valuetest7 -> setValue(d);
}
}
Thanks to everyone for your great help! Here is the final code:
void MainWindow::onGpsCalcLoop()
{
//Get initial values from gps run loop-------
double lat2 = flat2;
double lat1 = flat;
double lon2 = flon2;
double lon1 = flon;
//GPS distance and speed calculation -------
double dLat = .01745329 * (lat2 - lat1);
double dLon = .01745329 * (lon2 - lon1);
lat1 = .01745329 * lat1;
lat2 = .01745329 * lat2;
double a = qSin(dLat/2) * qSin(dLat/2) +
qSin(dLon/2) * qSin(dLon/2) * qCos(lat1) * qCos(lat2);
double c = 2 * qAtan2(qSqrt(a), qSqrt(1-a));
double d = 3975 * c;
//Distance calculation done. Next is total distance and speed------
td = td + d;
if (d > .1)
{
d = 0;
td = 0;
}
if (ui -> metric -> isChecked())
{
ui -> valuetest1 -> setValue(td * 1.6);
ui -> valuetest2 -> setValue((d * 3600) * 1.6);
}
else
{
ui -> valuetest1 -> setValue(td);
ui -> valuetest2 -> setValue(d * 3600);
}
flat2 = flat;
flon2 = flon;
}
I used .01745329*(whatever) instead of qDegreesToRadians(whatever) just to keep things as simple as possible.
Again, thanks to all for your help!

C++ to VB.NET Conversion: Assignment Within Expressions

Am converting some C++ code to VB.NET and need to convert assignments within expressions. Below are some C++ lines of code for which it's not clear what the converted results would be:
i2 = 1 + (i1 = i + i)
i4 = 1 + (i3 = n - i1)
wr = (wtemp = wr) * wpr - wi * wpi + wr
data(0) = (h1r = data(0)) + data(1)
data(0) = c1 * ((h1r = data(0)) + data(1))
Would the first line translate to:
If i2 = 1 Then i1 = i + i
?
Hans gave you the procedure - but just in case there's still any doubt about how to do this, your final result should be:
i1 = i + i
i2 = 1 + i1
i3 = n - i1
i4 = 1 + i3
wtemp = wr
wr = wtemp * wpr - wi * wpi + wr
h1r = data(0)
data(0) = h1r + data(1)
h1r = data(0)
data(0) = c1 * (h1r + data(1))
The code is already converted to VB.NET!
For example if you look at the following VB.NET code
Dim i2 As Int16
Dim i1 As Int16
Dim i As Int16
Dim data(0 To 1)
i = 1
i1 = 1
i2 = 0
i2 = 1 + (i1 = i + i) 'Same as your C++ code
MsgBox(i2)
It will return 1. The code translates to
i2 = 1 + (if i1= i+i)