SPSS Return Highest Variable Name - syntax

1) I need SPSS to return the variable name of the highest variable in a series of subscales. Basically I have ten subscales with mean scores ranging from 0 to 5, I don't want to know the highest score for each case, but rather which subscale is highest.
When I use this syntax, I just get the score, which doesn't tell me which category it belongs to.
COMPUTE Motivation_Highest2 = MAX(Stress_Mgmt, Revitalisation, Enjoyment, Challenge, SocialRecog, Affiliation, Competition, HealthPress, IllHealth, PosHealth,
WtMgmt, Appearance, StrengthEnd, Nimbleness, MotivationHighest).
VARIABLE LABELS Motivation_Highest2 'Motivation Intensity: Highest Score on any Motivation Subscale'.
EXECUTE.
Can I ask SPSS to return the variable name instead of the score?
2) There may be two scores that are both equally high. In this case, I would like SPSS to give me both variable names.
Thanks!

This is an ok job for a macro to do.
DEFINE !MaxVars (OutN = !TOKENS(1)
/OutV = !CHAREND("/")
/Var = !CMDEND)
NUMERIC !OutN.
!DO !I !IN (!Var)
COMPUTE !OutN = MAX(!OutN,!I).
!DOEND
STRING !OutV (!CONCAT("A",!LENGTH(!Var))).
!DO !I !IN (!Var)
IF !I = !OutN !OutV = LTRIM(CONCAT(RTRIM(!OutV)," ",!QUOTE(!I))).
!DOEND
!ENDDEFINE.
And here is an example of using it on a set of data.
DATA LIST FREE / X1 X2 X3.
BEGIN DATA
1 2 3
3 2 1
4 4 0
0 4 4
1 1 1
END DATA.
!MaxVars OutN = Max OutV = Vars /Var = X1 X2 X3.
If you then run LIST Max Vars. it will return in the output:
Max Vars
3 X3
3 X1
4 X1 X2
4 X2 X3
1 X1 X2 X3

Related

Algorithm to assign one activity to each one individual in order to maximize the sum of the affinity scores

Consider N individuals (x1, x2 ... xn) and M activities (y1, y2 ... ym).
For each pair (individual, activity) there is a score of affinity, a that relates them.
The objective is to assign an activity to each individual to maximize the sum of the affinity scores of the chosen pairs. An activity can be assigned to only one individual and an individual can be associated with only one activity.
Below is an example of a matrix that reports the affinity of each pair.
| x1 x2 x3
----+------------
y1 | 2 7 0
y2 | 7 7 9
y3 | 3 1 4
y4 | 5 4 4
I would like to know efficient algorithms that allow finding these pairs (individual, activity) that maximize the sum of affinities.
PS: I'm not an expert, so forgive me if that can have an obvious answer.

Genetic algorithm to solve a quadratic equation

I have a problem understanding the process for genetic algorithms. I found examples of maximizing a function over an interval, and I think I understand them, but how can a genetic algorithm be used to solve, for example, a quadratic equation?
Assuming that we want to find a solution up to 4 digits, what is a proper representation to encode the numbers? What can be used as the fitness function to evaluate each number?
Any help is appreciated
If you want to solve a quadratic equation
a * x^2 + b * x + c = 0
then you need only one variable x as representation. You can use
f(x) = abs(a * x^2 + b * x + c)
as fitness function, which is the same as the precision then, so it needs to be minimized.
But with only one variable it's hard to do crossovers, you can use 10 numbers per individual and then take the average to get x, or just take the average of the two numbers when doing crossovers. Also for mutation instead of completely overriding x, you could multiply it by a random number between 0.5 and 2 for example.
First step is choose a representation of solutions. The most widely used is binary encoding. For example your x may looks:
1 0 0 1 1 1 1 0 | 0 0 0 0 0 0 0 0 0 0 1 1 1
First 8 bits coded an integer part of number, residual 13 bits coded part of number after dot. In this example the binary string coding a number 158.0007.
Crrossover may looks
1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 - 158.0007
1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 - 225.0008
The most simple crossover operator is one divide point. You generate one number from 1 to length of string - 1. And to this point you get a bits from one string and from that point from second string. In this example we choose for divide point 4 position. The offspring will looks like:
1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 - 145.0008
Mutation change with chosen probability some bits.
Fitness function may be a function value of quadratic equation(in case you try found an maximum) in x and x is obtained as decoding of bits string.
And some theory on the end. You have a two sets. One set is search space(space with binary strings) and second set is space with solution. Individual from search space is decoded into the solution in the solution space(in our case value of x coded by binary string). Search space represent genotype and decoded solution is phenotype. Genetics operators work with search space individual(binary string in this case) and fitness function using a decoded solution.
I've got one that solves the equation:
a(x1*x1+x2*x2)+b(x1+x2)+2*c = 0
which is the addition of:
ax1x1+bx1+c=0 and ax2x2+bx2+c=0
since x1 and x2 are both the solutions of the equation the addition can be made. The code gives for aa=1, bb=-1 and cc=-30 the following output:
best solutions at generation 0 :: fitness = 1
chromosome 13 : x1 = -5 , x2 = 6
chromosome 269 : x1 = 6 , x2 = 6
chromosome 340 : x1 = 6 , x2 = -5
chromosome 440 : x1 = -5 , x2 = 6
chromosome 452 : x1 = 6 , x2 = -5
chromosome 549 : x1 = -5 , x2 = -5
chromosome 550 : x1 = 6 , x2 = -5
chromosome 603 : x1 = -5 , x2 = -5
chromosome 826 : x1 = 6 , x2 = -5
chromosome 827 : x1 = -5 , x2 = 6
chromosome 842 : x1 = -5 , x2 = -5
chromosome 952 : x1 = 6 , x2 = 6
chromosome 986 : x1 = 6 , x2 = -5
which is, I believe a good start, I only doesn't know yet how to filter the good from the less good solutions.
this is the code partially:
void objective(Chromosome* c){
// the problem here is when one root is found the fitness
// will be 1 :
// resulting in the second value is a non-root or the same
// value as the first root
//so probably I need to rewrite the fitness function
c->result = aa * ((c->gene[0].geneticcode * c->gene[0].geneticcode) + (c->gene[1].geneticcode * c->gene[1].geneticcode)) /
+ bb * (c->gene[0].geneticcode + c->gene[1].geneticcode) /
+ 2 * cc;
}
void fitness(Chromosome* c){
//rewrite of fitness function for this example
c->fitness = 1.0 / (1.0 + fabs(c->result));
}
If anyone can improve and I'm sure there are please share.

A feature ranking algorithm

if I have the following partitions or subsets with the corresponding scores as follows:
{X1,X2} with score C1
{X2,X3} with score C2
{X3,X4} with score C3
{X4,X1} with score C4
I want to write an algorithm that will rank the Xs based on the corresponding score of the subset they appeared in.
one way for example will be to do the following:
X1 = (C1 + C4)/2
X2 = (C1 + C2)/2
X3 = (C2 + C3)/2
X4 = (C3 + C4)/2
and then sort the results.
is there a more efficient or better ideas to do the ranking?
If you think that the score of a set is the sum of the scores of each object, you can write your equation in matrix form as :
C = M * X
where C is a vector of length 4 with components C1, C2, C3, C4, M is the matrix (in your case, as I understand this may vary)
1 1 0 0
0 1 1 0
0 0 1 1
1 0 0 1
and X is the unknown. You can then use Gaussian elimination to determine X and the get the ranking as you suggested.

distributing apples and oranges into boxes. Confused about solution

How many ways are there to discribute 4 identical oranges and 6 distinct apples into 5 distinct boxes
I know you find number of ways for apples which is 5^6.
The solution tells me that the ways for oranges is 8 choose 4.
You then multiply both of them together to get solution, which is 1,093,750
My question is....where does 8 come from, for 8 choose 4???
The number of ways you can stick the 4 identical oranges into 5 distinct boxes is the same as the number of nonnegative integer solutions to the problem:
x1 + x2 + x3 + x4 + x5 = 4
Suppose I take 4 1's and 4 separating +'s. Then there is a one-to-one correspondence between orderings of 1's and +'s and solutions of the equation.
For example, "1++11+1+" corresponds to "1 + 0 + 2 + 1 + 0".
There are 8! ways to order these 8 symbols, and because there are 4 identical 1's and 4 identical +'s we need to divide by 4! twice.
8! / (4! × 4!) = 8 choose 4
In general, the number of ways to put n identical objects into r distinct bins is [(n + r - 1) choose (r - 1)]. (r - 1 is referring to the number of "separating '+' symbols.")
Although there is a easy mathematical solution I give you the programming solution because other answer would be of topic:
consider every possible arrangement of oranges in boxes, and do it with recursion:
1.no orange in the last box: it is similar to you have 4 similar oranges and 4 distinct boxes.
2.one orange in the last box: it is similar to you have 3 similar oranges and 4 distinct boxes.
and so on ...
now for making it to program use a function like this:
int orangeRecursion(int numOfOranges,int numOfBoxes){
if(numOfOranges == 0)
return 1;
if(numOfBoxes == 0)
return 0;
int num = 0;
for(int i = 0 ; i<=numOfOranges ; i++){
num += orangeRecursion(numOfOranges - i,numOfBoxes - 1)
}
return num;
}

Matrix linear indexing

I need to traverse a rectangular grid in continuous manner. Here is an example of what I want, the number means sequence:
+ x
y 0 1 2
5 4 3
6 7 8
At each step I know the index in matrix. Is there any way to calculate the coordinates? The inverse mapping for [x + y * width] doesn't help, beacuse it creates "steps" or "jumps". Is there any solution?
Here is explanation for "steps" mentioned above:
+ x
y 0 1 2
3 4 5 //at this moment the X coordinate changes by 3, thus create step
6 7 8
y = index / width
if( y % 2 == 0 )
x = index % width
else
x = width - index % width - 1
I think that should do it. It's a single modification of the standard way of calculating with "steps" as you call them. You are only changing the way the calculation is done based upon the row.
so you need to first increase the "x" component and then decrease right - so that you get a kind of snake-behavior? You will need an if statement (or some kind of modulo - magic). Let my try the magic:
y := floor(i/columnCount)
x = (y mod 2)*(i - y*columCount) + ((y+1) mod 2)*((columnCount -1) - (i - y*columnCount))

Resources