Extracting frequency with custom tables for multi-response questions in SPSS - response

I have the following the answers to a multi-response question in a sav file
p85_1 |p85_2 |p85_3
-------------------
1 | 1 | 0
1 | 0 | 0
0 | 1 | 0
1 | 1 | 1
1 | 1 | 0
I need to retrieve the frequency(-distribution) but using/through custom tables and SPSS syntax.

Define this as a multiple response set (Analyze > Tables > Multiple Response Sets) and then drag that set onto the Custom Tables canvas.

Related

SPSS: How would I create a column summing up means / medians / range from Compare Means function?

I'm trying to sum up across a row for different numerical variables that have been processed through the Compare Means function.
Below (without the last 'Total' column') is what I have generated from Compare Means; I'm looking to generate the last Total column.
+--------+-------+-------+-------+-------+
| | Var 1 | Var 2 | Var 3 | Total |
+--------+-------+-------+-------+-------+
| Mean | 10 | 1 | 2 | |
| Median | 4 | 20 | 4 | |
| Range | 6 | 40 | 1 | |
| Std.dev| 3 | 3 | 3 | |
+--------+-------+-------+-------+-------+
Here's the syntax of my command:
MEANS TABLES=VAR_1 VAR_2 VAR_3
/CELLS=MEAN STDDEV MEDIAN RANGE.
Can't really imagine what the use is for summing these values, but forget about why - this is how:
The OMS command takes results from the output and puts them in a new dataset which you can then further analyse, as you requested.
DATASET DECLARE MyResults.
OMS /SELECT TABLES /IF COMMANDS=['Means'] SUBTYPES=['Report'] /DESTINATION FORMAT=SAV OUTFILE='MyResults' .
* now your original code.
MEANS TABLES=VAR_1 VAR_2 VAR_3 /CELLS=MEAN STDDEV MEDIAN RANGE.
* now your results are captured - we'll go see them.
omsend.
dataset activate MyResults.
* the results are now in a new dataset, which you can analyse.
compute total=sum(VAR_1, VAR_2, VAR_3).
exe.

How do I conditionally highlight cells based off data from another sheet?

I have two tabs in a Google Sheets file. One contains data to display (auto-generated), and another contains information about availability. They appear as such, where the sheets correspond 1-to-1:
User tally
A B C D
+----------+------+------+------+
1 | | OS 1 | OS 2 | OS 3 |
+----------+------+------+------+
2 | Device A | 12 | 0 | 512 |
+----------+------+------+------+
3 | Device B | 0 | 156 | 18 |
+----------+------+------+------+
4 | Device C | 0 | 0 | 0 |
+----------+------+------+------+
OS availability
A B C D
+----------+------+------+------+
1 | | OS 1 | OS 2 | OS 3 |
+----------+------+------+------+
2 | Device A | 1 | 0 | 1 |
+----------+------+------+------+
3 | Device B | 0 | 1 | 1 |
+----------+------+------+------+
4 | Device C | 0 | 0 | 1 |
+----------+------+------+------+
As you can see, the Devices for which an OS is not supported all have 0 users, as expected. However, there are some Devices that do support an OS, but 0 users have that combination (i.e. Device B on OS 3, and Device C entirely).
In my User tally spreadsheet, I want to use Conditional Formatting to change the background and text color of unsupported combinations to the same thing, making it appear as if the cell is completely blank. However, I want supported combinations to display their 0, indicating that users can use this combination, but don't for some reason.
I tried placing the following into the Conditional Formatting panel for 'User tally'!B2:D4 as a Custom formula is (hoping B2 would change per-cell like it does when you paste a formula across many cells), but it didn't appear to have an effect:
EQUAL('OS support'!B2, '0')
How do I use Conditional Formatting to change the color of each cell based on counterpart data from another table?
Place the following into the Conditional Formatting panel for 'User tally'!B2:D4 as a Custom formula:
=OFFSET(INDIRECT("'OS support'!A1"),row(B2)-1,COLUMN(B2)-1)=0

find rows where there is another row with an opposite value in the table

Im trying to find an efficient way to solve the problem:
I need to find all rows in a table where there is another row with an opposite column value.
For example I have transactions with columns id and amount
| id | amount |
|----|--------|
| 1 | 1 |
| 2 | -1 |
| 3 | 2 |
| 4 | -2 |
| 5 | 3 |
| 6 | 4 |
| 7 | 5 |
| 8 | 6 |
The query should return only the first 4 rows:
| id | amount |
|----|--------|
| 1 | 1 |
| 2 | -1 |
| 3 | 2 |
| 4 | -2 |
My current solution is terribly efficient as I am going through 1000's of transactions:
transactions.find_each do |transaction|
unless transactions.where("amount = #{transaction.amount * -1}").count > 0
transactions = transactions.where.not(amount: transaction.amount).order("# amount DESC")
end
end
transactions
Are there any built in Rails or Postgresql functions that could help with this?
Use following query:
SELECT DISTINCT t1.*
FROM transactions t1
INNER JOIN transactions t2 ON t1.amount = t2.amount * -1;
SELECT * FROM the_table t
WHERE EXISTS (
SELECT * FROM the_table x
WHERE x.amount = -1*t.amount
-- AND x.amount > t.amount
);
Consider storing an absolute value indexed column then query for the positive value. Postgres has an absolute value function; but I think the beauty of ActiveRecord is that Arel abstracts away the SQL. DB specific SQL can be a pain if you change later.
There is type called abs which will return irrespective of symobol. From my example data is the table name
SELECT id,amount FROM DATA WHERE id = ABS(amount)
This is the sample test table
Here is the output

How to perform batch update of a column within a range in psql

Here is my table structure:
Table name:Items
--------------------------------
id | category_id | code |
--------------------------------
1 | 1 | 15156 |
2 | 1 | 15157 |
2 | 1 | 15158 |
2 | 1 | 15159 |
2 | 1 | 15160 |
2 | 1 | 15161 |
Here code field is unique and its type is string. I need to increment code field values by +1(code field is string).
You can try
Item.update_all(code: "#{code.to_i + 1}")
If you want to read update_all
The update_all won't work because the record attributes are not available.
Better might be...
minimum = "15157"
maximum = "15160"
Item.where("code >= ? AND code <= ?", minimum, maximum).each{|i| i.update_attribute(:code, "#{i.code.to_i + 1}") }
(edited to reflect two arguments passed to update_attribute)
Edited to reflect #rustamagasanov suggestion to limit to a given range of code values...

SPSS: check if an element of some variable exist in / is also element of another variable

I want compute a variable which checks if an element of variable v1 is also contained in another variable v2.
Let's assume the following example:
I have the following three variables: group-ID (gID), person-ID (pID) (within the group) and lender-ID (LID), the ID of the person which lent money.
I then want to create a new variable (loa) which indicates for every person if this person has lent money to any other person. Or in other words, if the element of pID also appears in LID.
As an example see the following table:
+-----+-----+-----+ +-----+-----+-----++-----+
+ gID | pID | LID | + gID | pID | LID || loa |
+-----+-----+-----+ +-----+-----+-----++-----+
+ 1 | 1 | 2 | + 1 | 1 | 2 ++ 0 |
+ 1 | 2 | - | + 1 | 2 | - || 1 |
+ 1 | 3 | 4 | + 1 | 3 | 4 || 0 |
+ 1 | 4 | - | + 1 | 4 | - || 1 |
+-----+-----+-----+ => +-----+-----+-----++-----+
+ 2 | 1 | - | + 2 | 1 | - || 1 |
+ 2 | 2 | 4 | + 2 | 2 | 4 || 0 |
+ 2 | 3 | 4 | + 2 | 3 | 4 || 0 |
+ 2 | 4 | 1 | + 2 | 4 | 1 || 1 |
How can this be done in SPSS?
The basic idea here is to create a dataset which is a set of occuring elements in LID (for each groupId), which means that in this data set every combination of groupiDs and LIDs (The Id of Persons who lent money) exist only one time.
One way to do so is to use the AGGREGATE command:
* Name the original dataset, so it can be addressed later.
DATASET NAME main.
* Create a data set with unique combinations of groupID and LoanID.
* Bonus: Count how many loans a person has given.
DATASET DECLARE set.
AGGREGATE OUTFILE=set
/BREAK gID LID
/count_loans = N.
This data set can now be used as a lookup to identify the persons (pID) who loaned money. The MATCH FILES command does the job:
* Indicate wether pID has lent money (exist as LID in the set data) or not.
* Bonus: Add variable which counts the loans of that person.
* Note: "main" has to be sorted by "gID pID"
* and "set" has to be sorted by "gID LID".
DATASET ACTIVATE main.
MATCH FILES
/FILE *
/TABLE set
/RENAME (lId=pID)
/IN loa
/BY gID pID.
EXECUTE.

Resources