How to combine several input data from items to one (string) variable in ztree? - ztree

In my experiment, users have the choice between 10 items per round they can select or leave the checkbox emtpy. In the next step, I'd like to create a new variable, e.g. MyInputR1, which holds the values of the previous checkboxes in the right order and as 1 new number.
My approach so far:
a)
Formatting input data: f1=format(D11,0.2).
Combining the input data and storing the information in a new variable: f = f1 + f2 + f3 + ....
Creating the variable MyInputR1 = stringtonumber(f)
b) Combining the input data (with values 0 or 1): MyInputR1 = D11 + D12 + D13 + D14 + ...
Unfortunately, the logic does not sum up and ztree does not understand what I am trying to do.
Thus my question:
Is it possible to combine / string together input data into 1 new variable, instead of adding it up?
Input data: checkbox with values 0 or 1
in total 10 input variables (D11 - D110)
Looking for a variable that e.g. looks like this: MyInputR1 = 0000011111
ztree code
Thanks for your help!

Related

Selecting a cut-off score in SPSS

I have 5 variables for one questionnaire about social support. I want to define the group with low vs. high support. According to the authors low support is defined as a sum score <= 18 AND two items scoring <= 3.
It would be great to get a dummy variable which shows which people are low vs high in support.
How can I do this in the syntax?
Thanks ;)
Assuming your variables are named Var1, Var2 .... Var5, and that they are consecutive in the dataset, this should work:
recode Var1 to Var5 (1 2 3=1)(4 thr hi=0) into L1 to L5.
compute LowSupport = sum(Var1 to Var5) <= 18 and sum(L1 to L5)>=2.
execute.
New variable LowSupport will have value 1 for rows that have the parameters you defined and 0 for other rows.
Note: If your variables are not consecutive you'll have to list all of them instead of using Var1 to var5.

Google Sheets: adding cells to sum when condition is met

What formula should I use in Google Sheets to get a sum of multiple cells, where every cell has an independent condition determining wether it should be included in the sum or not. Something like this:
result = 0 + (IF(condition1, A1)) + (IF(condition2, B1)) + (IF(condition3, C1))
Depending on your conditions, you can use something similar to this:
=SUM(IF(A1<>0,A1,0),IF(B1<10,B1,0),IF(C1<10,C1,0),D1)
Therefore, as a general example, you can simply make use of the SUM and the IF functions:
=SUM(IF(CONDITION1,A1,0),IF(CONDITION2,B1,0),IF(CONDITION3,C1,0)...)
the elements of the SUM function are separated by a comma;
the IF returns the first value if the condition is met and the second one if the condition is not met.
Reference
SUM function;
IF function.
Turns out it works the way I posted in Original Question:
result = 0 + (IF(condition1, A1)) + (IF(condition2, B1)) + (IF(condition3, C1))
BUT you need to use a different parameter separator if comma (,) is used as a decimal point separator in your language. For Polish I had to use semicolon (;)
result = 0 + (IF(condition1; A1)) + (IF(condition2; B1)) + (IF(condition3; C1))

How do I use COMPUTE to create and calculate a series of values in SPSS?

I need to compute 61 new variables using a simple math equation based on 4 sets of 61 existing variables. I know I can write 61 compute statements. Is there a more elegant way of creating these variables? Here's how the 61 statements would look:
COMPUTE score_1 = factor_1 * (a_1 + b_1) + c_1.
...
COMPUTE score_61 = factor_61 * (a_61 + b_61) + c_61.
EXECUTE.
Thanks in advance.
recode accept to and numbers my new variables (recode raw1 to raw61 (1=0) (2=1) into a_1 to a_61.) Can I do the same here?
You can use a do repeat structure
DO REPEAT score=score_1 score_2 ... score_61
/factor = factor_1 factor_2 ... factor_61
/a=a_1 a_2 ... a_61
/b=b_1 b_2 ... b_61
/c=c_1 c_2 ... c_61.
COMPUTE score=factor*(a+b)+c.
END REPEAT.
EXECUTE.
In the fortunate event that your variables are in set order (i.e. - all factors are consecutive, all a are consecutive, etc. you may reference them usingto like this:
/factor = factor_1 TO factor_61
otherwise, you need to enumerate them one by one. Hope this helps

Variable for the number of cases SPSS

In my SPSS Syntax Script I compute a bunch of formulas for each cases.
Let' say this is my data:
id value
1 34
2 12
3 94
I now compute a new variable where I need the number of cases in the file (number of ids)
So
COMPUTE newvar = value/ NUMBER OF CASES
in this example NUMBER OF CASES would be 3.
Is there a command for this? thx
You can use the AGGREGATE command without a break variable to return the number of cases in the dataset. Example below:
DATA LIST FREE / ID Value.
BEGIN DATA
1 34
2 12
3 94
END DATA.
AGGREGATE OUTFILE=* MODE=ADDVARIABLES
/BREAK
/NumberOfCases=N.
COMPUTE NewVar = Value/NumberOfCases.

Constrained Sequence to Index Mapping

I'm puzzling over how to map a set of sequences to consecutive integers.
All the sequences follow this rule:
A_0 = 1
A_n >= 1
A_n <= max(A_0 .. A_n-1) + 1
I'm looking for a solution that will be able to, given such a sequence, compute a integer for doing a lookup into a table and given an index into the table, generate the sequence.
Example: for length 3, there are 5 the valid sequences. A fast function for doing the following map (preferably in both direction) would be a good solution
1,1,1 0
1,1,2 1
1,2,1 2
1,2,2 3
1,2,3 4
The point of the exercise is to get a packed table with a 1-1 mapping between valid sequences and cells.
The size of the set in bounded only by the number of unique sequences possible.
I don't know now what the length of the sequence will be but it will be a small, <12, constant known in advance.
I'll get to this sooner or later, but though I'd throw it out for the community to have "fun" with in the meantime.
these are different valid sequences
1,1,2,3,2,1,4
1,1,2,3,1,2,4
1,2,3,4,5,6,7
1,1,1,1,2,3,2
these are not
1,2,2,4
2,
1,1,2,3,5
Related to this
There is a natural sequence indexing, but no so easy to calculate.
Let look for A_n for n>0, since A_0 = 1.
Indexing is done in 2 steps.
Part 1:
Group sequences by places where A_n = max(A_0 .. A_n-1) + 1. Call these places steps.
On steps are consecutive numbers (2,3,4,5,...).
On non-step places we can put numbers from 1 to number of steps with index less than k.
Each group can be represent as binary string where 1 is step and 0 non-step. E.g. 001001010 means group with 112aa3b4c, a<=2, b<=3, c<=4. Because, groups are indexed with binary number there is natural indexing of groups. From 0 to 2^length - 1. Lets call value of group binary representation group order.
Part 2:
Index sequences inside a group. Since groups define step positions, only numbers on non-step positions are variable, and they are variable in defined ranges. With that it is easy to index sequence of given group inside that group, with lexicographical order of variable places.
It is easy to calculate number of sequences in one group. It is number of form 1^i_1 * 2^i_2 * 3^i_3 * ....
Combining:
This gives a 2 part key: <Steps, Group> this then needs to be mapped to the integers. To do that we have to find how many sequences are in groups that have order less than some value. For that, lets first find how many sequences are in groups of given length. That can be computed passing through all groups and summing number of sequences or similar with recurrence. Let T(l, n) be number of sequences of length l (A_0 is omitted ) where maximal value of first element can be n+1. Than holds:
T(l,n) = n*T(l-1,n) + T(l-1,n+1)
T(1,n) = n
Because l + n <= sequence length + 1 there are ~sequence_length^2/2 T(l,n) values, which can be easily calculated.
Next is to calculate number of sequences in groups of order less or equal than given value. That can be done with summing of T(l,n) values. E.g. number of sequences in groups with order <= 1001010 binary, is equal to
T(7,1) + # for 1000000
2^2 * T(4,2) + # for 001000
2^2 * 3 * T(2,3) # for 010
Optimizations:
This will give a mapping but the direct implementation for combining the key parts is >O(1) at best. On the other hand, the Steps portion of the key is small and by computing the range of Groups for each Steps value, a lookup table can reduce this to O(1).
I'm not 100% sure about upper formula, but it should be something like it.
With these remarks and recurrence it is possible to make functions sequence -> index and index -> sequence. But not so trivial :-)
I think hash with out sorting should be the thing.
As A0 always start with 0, may be I think we can think of the sequence as an number with base 12 and use its base 10 as the key for look up. ( Still not sure about this).
This is a python function which can do the job for you assuming you got these values stored in a file and you pass the lines to the function
def valid_lines(lines):
for line in lines:
line = line.split(",")
if line[0] == 1 and line[-1] and line[-1] <= max(line)+1:
yield line
lines = (line for line in open('/tmp/numbers.txt'))
for valid_line in valid_lines(lines):
print valid_line
Given the sequence, I would sort it, then use the hash of the sorted sequence as the index of the table.

Resources