Table displaying the percentages of chosen responses for a vingette (male then female) - response

People in my study have completed a questionnaire. One of the questions involves the participant reading a scenario/vingette and then they are asked to 'identify the problem' in the scenario. They are then presented with a 9 options (multiple response question).
In my data file men and women are coded numerically. I am currently trying to create a table with the percentage of responses to each of the 9 options for men in one column (adding to 100%), and women in the other (100%).
I know this is probably quite simple, but I've completely forgotten! Any help in exactly how to carry this out in SPSS?

You Can Use the MULT RESPONSE GROUPS Command with the /BASE=RESPONSES option.
*** Create some example fake data.
INPUT PROGRAM.
LOOP #i = 1 TO 15.
LOOP #g = 1 TO 2.
COMPUTE Gender = #g.
DO REPEAT response = r1 TO r9.
COMPUTE response = TRUNC(RV.UNIFORM(0,2)).
END REPEAT.
END CASE.
END LOOP.
END LOOP.
END FILE.
END INPUT PROGRAM.
FORMATS Gender r1 TO r9 (F1.0).
VALUE LABELS Gender
1 'Male'
2 'Female'.
EXECUTE.
And that's how the command looks like with the example data:
MULT RESPONSE GROUPS=$responses (r1 r2 r3 r4 r5 r6 r7 r8 r9 (1))
/VARIABLES=Gender(1 2)
/TABLES=$responses BY Gender
/BASE=RESPONSES
/CELLS=COLUMN.

It sounds like you can get what you want from the CROSSTABS procedure, see example below and image it produces with my default settings.
*Example fake data.
INPUT PROGRAM.
LOOP #i = 1 TO 15.
LOOP #g = 1 TO 2.
COMPUTE Gender = #g.
COMPUTE Response = TRUNC(RV.UNIFORM(1,10)).
END CASE.
END LOOP.
END LOOP.
END FILE.
END INPUT PROGRAM.
FORMATS Gender Response (F1.0).
VALUE LABELS Gender
1 'Male'
2 'Female'.
*Table.
CROSSTABS TABLE Response BY GENDER /CELLS=COL.

Related

Reading a column file of x y z into table in Lua

Been trying to find my way through Lua, so I have a file containing N lines of numbers, 3 per line, it is actually x,y,z coordinates. I could make it a CSV file and use some Lua CSV parser, but I guess it's better if I learn how to do this regardless.
So what would be the best way to deal with this? So far I am able to read each line into a table line by the code snippet below, but 1) I don't know if this is a string or number table, 2) if I print tbllinesx[1], it prints the whole line of three numbers. I would like to be able to have tbllines[1][1], tbllines[1][2] and tbllines[1][3] corresponding to the first 3 number of 1st line of my file.
local file = io.open("locations.txt")
local tbllinesx = {}
local i = 0
if file then
for line in file:lines() do
i = i + 1
tbllinesx[i] = line
end
file:close()
else
error('file not found')
end
From Programming in Lua https://www.lua.org/pil/21.1.html
You can call read with multiple options; for each argument, the
function will return the respective result. Suppose you have a file
with three numbers per line:
6.0 -3.23 15e12
4.3 234 1000001
... Now you want to print the maximum of each line. You can read all three numbers in a single call to read:
while true do
local n1, n2, n3 = io.read("*number", "*number", "*number")
if not n1 then break end
print(math.max(n1, n2, n3))
end
In any case, you should always consider the alternative of reading the
whole file with option "*all" from io.read and then using
gfind to break it up:
local pat = "(%S+)%s+(%S+)%s+(%S+)%s+"
for n1, n2, n3 in string.gfind(io.read("*all"), pat) do
print(math.max(n1, n2, n3))
end
I'm sure you can figure out how to modify this to put the numbers into table fields on your own.
If you're using three captures you can just use table.pack to create your line table with three entries.
Assuming you only have valid lines in your data file (locations.txt) all you need is change the line:
tbllinesx[i] = line
to:
tbllinesx[i] = { line:match '(%d+)%s+(%d+)%s+(%d+)' }
This will put each of the three space-delimited numbers into its own spot in a table for each line separately.
Edit: The repeated %d+ part of the pattern will need to be adjusted according to your actual input. %d+ assumes plain integers, you need something more involved for possible minus sign (%-?%d+) and for possible dot (%-?%d-%.?%d+), and so on. Of course the easy way would be to grab everything that is not space (%S+) as a potential number.

How to randomly get a value from a table [duplicate]

I am working on programming a Markov chain in Lua, and one element of this requires me to uniformly generate random numbers. Here is a simplified example to illustrate my question:
example = function(x)
local r = math.random(1,10)
print(r)
return x[r]
end
exampleArray = {"a","b","c","d","e","f","g","h","i","j"}
print(example(exampleArray))
My issue is that when I re-run this program multiple times (mash F5) the exact same random number is generated resulting in the example function selecting the exact same array element. However, if I include many calls to the example function within the single program by repeating the print line at the end many times I get suitable random results.
This is not my intention as a proper Markov pseudo-random text generator should be able to run the same program with the same inputs multiple times and output different pseudo-random text every time. I have tried resetting the seed using math.randomseed(os.time()) and this makes it so the random number distribution is no longer uniform. My goal is to be able to re-run the above program and receive a randomly selected number every time.
You need to run math.randomseed() once before using math.random(), like this:
math.randomseed(os.time())
From your comment that you saw the first number is still the same. This is caused by the implementation of random generator in some platforms.
The solution is to pop some random numbers before using them for real:
math.randomseed(os.time())
math.random(); math.random(); math.random()
Note that the standard C library random() is usually not so uniformly random, a better solution is to use a better random generator if your platform provides one.
Reference: Lua Math Library
Standard C random numbers generator used in Lua isn't guananteed to be good for simulation. The words "Markov chain" suggest that you may need a better one. Here's a generator widely used for Monte-Carlo calculations:
local A1, A2 = 727595, 798405 -- 5^17=D20*A1+A2
local D20, D40 = 1048576, 1099511627776 -- 2^20, 2^40
local X1, X2 = 0, 1
function rand()
local U = X2*A2
local V = (X1*A2 + X2*A1) % D20
V = (V*D20 + U) % D40
X1 = math.floor(V/D20)
X2 = V - X1*D20
return V/D40
end
It generates a number between 0 and 1, so r = math.floor(rand()*10) + 1 would go into your example.
(That's multiplicative random number generator with period 2^38, multiplier 5^17 and modulo 2^40, original Pascal code by http://osmf.sscc.ru/~smp/)
math.randomseed(os.clock()*100000000000)
for i=1,3 do
math.random(10000, 65000)
end
Always results in new random numbers. Changing the seed value will ensure randomness. Don't follow os.time() because it is the epoch time and changes after one second but os.clock() won't have the same value at any close instance.
There's the Luaossl library solution: (https://github.com/wahern/luaossl)
local rand = require "openssl.rand"
local randominteger
if rand.ready() then -- rand has been properly seeded
-- Returns a cryptographically strong uniform random integer in the interval [0, n−1].
randominteger = rand.uniform(99) + 1 -- randomizes an integer from range 1 to 100
end
http://25thandclement.com/~william/projects/luaossl.pdf

Possible to use less/greater than operators with IF ANY?

Is it possible to use <,> operators with the if any function? Something like this:
select if (any(>10,Q1) AND any(<2,Q2 to Q10))
You definitely need to create an auxiliary variable to do this.
#Jignesh Sutar's solution is one that works fine. However there are often multiple ways in SPSS to accomplish a certain task.
Here is another solution where the COUNT command comes in handy.
It is important to note that the following solution assumes that the values of the variables are integers. If you have float values (1.5 for instance) you'll get a wrong result.
* count occurrences where Q2 to Q10 is less then 2.
COUNT #QLT2 = Q2 TO Q10 (LOWEST THRU 1).
* select if Q1>10 and
* there is at least one occurrence where Q2 to Q10 is less then 2.
SELECT (Q1>10 AND #QLT2>0).
There is also a variant for this sort of solution that deals with float variables correctly. But I think it is less intuitive though.
* count occurrences where Q2 to Q10 is 2 or higher.
COUNT #QGE2 = Q2 TO Q10 (2 THRU HIGHEST).
* select if Q1>10 and
* not every occurences of (the 9 variables) Q2 to Q10 is two or higher.
SELECT IF (Q1>10 AND #QGE2<9).
Note: Variables beginning with # are temporary variables. They are not stored in the data set.
I don't think you can (would be nice if you could - you can do something similar in Excel with COUNTIF & SUMIF IIRC).
You've have to construct a new variable which tests the multiple ANY less than condition, as per below example:
input program.
loop #j = 1 to 1000.
compute ID=#j.
vector Q(10).
loop #i = 1 to 10.
compute Q(#i) = trunc(rv.uniform(-20,20)).
end loop.
end case.
end loop.
end file.
end input program.
execute.
vector Q=Q2 to Q10.
loop #i=1 to 9 if Q(#i)<2.
compute #QLT2=1.
end loop if Q(#i)<2.
select if (Q1>10 and #QLT2=1).
exe.

Ruby, User id with 6 random numbers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
For example: If I'm creating a new user in Rails, the id number will be 1, 2, 3 and so on. In the URL the users id will be like "users/1". My question is: How do I make a random number for every new users? So the id won't be 1, 2, 3 and so on. More like id: 643893, name: Carl, age: 34?
If you really insist upon the digits, you could do something like this
def random_id
Random.new.bytes(8).bytes.join[0,8]
end
5.times { puts random_id }
Output
19854517
11802301
16391219
18414719
35583913
Anyway, I believe you have a bit of an XY Problem and your end goal is to just generate unique ids for your users. The function above is going to be random but without checking with your database, you'll have no idea whether the result is unique or not. Here's some alternative approaches I'd take:
If you wanted, it's not 8 digits, but you could slice of the beginning of a uuid
require "securerandom"
SecureRandom.uuid.split("-").first
# b05582d8
That said, I think it's a much better to use the whole thing
SecureRandom.uuid
# b05582d8-3489-4bba-b94f-565ee458b1f9
Since you asked for user ids, I'm going to assume
There should be no collisions in the user ids.
Because of this, you don't want just purely random ids, otherwise you'll end up with a collision as a result of the birthday problem. Out of only 10^8 random userids, this means the odds are significant that a collision will occur after around 10^4 users, and it is always possible it may happen earlier than this.
What you are looking for is a permutation function. Using some basic number theory, here's one simple, non-secure way to do it:
N = 99999999 # set of ordered numbers to permute = {0,1,2,...,N-1}
P = 1000000007 # prime number larger than N
A = 181932213 # random integer: 2 <= A < P
B = 611232645 # random integer: 0 <= B < P
def get_user_id( i )
j = i.instance_of?(Fixnum) ? i : i.dup # i.dup if i is a Bignum
j -= 1
begin j = ( A * j + B ).modulo P end until j < N
j + 1
end
Internally, you still want to generate incremental ids id. Then get_user_id(id) with return an 8-digit number (or less) which you can use as the user_id, which is guaranteed to not conflict with any other user_id for all non-zero 8-digit numbers id.
To illustrate:
1.upto(N) do |i|
j = get_user_id(i)
puts "#{i}\t#{j}"
end
Output:
1 41168842
2 45894710
3 11122313
4 74043578
5 78434518
6 72415977
...
Note that anyone who knows/suspects you are using this algorithm can fairly easily determine what N, P, A, and B are from getting some consecutively generated values, and obtain the underlying id value which you wanted to keep hidden. If you need something more secure than this, then consider looking into permutation ciphers.
Below is the example, which start with alphabet.
def self.generate_reg_no
#reg_no = PatientReg.last
#reg_no.blank? ? (#reg_no = 'REG00') : (#reg_no = #reg_no.reg_no)
new_id =""
n = #reg_no.length
last_count = #reg_no.slice!(4..n)
new_id << "REG0" << (last_count.to_i+1).to_s
end
You can modify as per you requirement.

Writing an If condition within a Loop in SPSS

I want to have a if condition within a loop. That is As long as id < 10,
check if Modc_initial is equal to MODC, if true then set d = 12
This is the code I tried bit not working, can anyone please help.
LOOP if (id LT 10)
IF(Modc_initial EQ MODC))
COMPUTE d = 12.
END LOOP.
EXECUTE.
You can either use a one line conditional of the form IF (condition) d = 12. or a multiple line DO IF. Below I provide an example of DO IF adapted to your syntax.
data list free / id MODC Modc_initial.
begin data
1 3 3
2 3 5
12 1 1
end data.
LOOP if (id LT 10).
DO IF (Modc_initial EQ MODC).
COMPUTE d = 12.
END IF.
END LOOP IF (d = 12).
EXECUTE.
Note you had a period missing in your original syntax on the initial LOOP. I also added an end loop condition, otherwise the code as written would just go until the maximum set number of loops per your system.

Resources