Hi all,
I'm looking to compute a new variable in SPSS that is the mode of a few other variables in my dataset. I thought I would be able to use some form of the syntax below... compute newvar=mode(var1, var2, var3). This isn't working for me. I've done some research to see if I can do this, but I'm not getting anywhere. It seems to me that there would have to be a relatively painless way to do this, but I can't find it. Any help would be appreciated. Thank you, Sarah |
Administrator
|
Hi Sarah. Off the top of my head, the only way I can think of to compute a mode in SPSS is via FREQUENCIES, like this:
FREQUENCIES VARIABLES = {varlist} / FORMAT=NOTABLE / STATISTICS=MODE . This suggests the following approach: 1. Restructuring your file with VARSTOCASES to get your var1-var3 values in a single variable (with 3 rows per ID). 2. Splitting the file by ID, and obtaining the modes as shown above, but sending the output to another dataset via OMS. 3. Doing a bit of data management on the dataset created via OMS (i.e., reducing it to one row per ID). 4. Reverting to the original data file, and bringing in the modes via MATCH FILES. But note! If there are two (or more) modes, FREQUENCIES only reports the first. HTH.
--
Bruce Weaver bweaver@lakeheadu.ca http://sites.google.com/a/lakeheadu.ca/bweaver/ "When all else fails, RTFM." PLEASE NOTE THE FOLLOWING: 1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above. 2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/). |
Administrator
|
;-) Too many notes Maestro ;-))) --- How about: Determine the counts of possible values across variables, determine which value is associated with the maximum count. identify multimodality by counting the number of occurrences of the max count. No data reshape, no OMS (no freaking python either ;-))). -- ** Simulate some data **. INPUT PROGRAM. LOOP CASEID=1 TO 100. DO REPEAT V=V1 TO V4. COMPUTE V=TRUNC(UNIFORM(5))+1. END REPEAT. END CASE. END LOOP. END FILE. END INPUT PROGRAM. EXE. *Calculate MODAL value across a set of variables (V1...V4) having values 1..5. * Creates another variable N_Mode indicating # of values tied for mode *. COMPUTE MAX_C=0. DO REPEAT C=#COUNTS1 TO #COUNTS5 /Value=1 TO 5. + COUNT C=V1 TO V4 (Value). + DO IF C GT MAX_C. + COMPUTE MAX_C=C. + COMPUTE MODE=Value. + END IF. END REPEAT. DO REPEAT Value=1 TO 4. + DO IF MAX_C=Value. + COUNT N_MODE=#COUNTS1 TO #COUNTS5 (Value). + END IF. END REPEAT. FORMATS ALL (F1.0). EXE.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
To note, David's solution only works if the variables are integer values and within a known integer value range. I ended up writing a solution that utilizes the reshaping Bruce suggested (although no need for OMS, I just got by with aggregate commands) but then realized this is probably a reasonable assumption on David's part (e.g. it works for likert responses, and when else is the modal value useful?)
I can post it if David's solution doesn't fit the OP's bill, but it would be best to be explicit about how your data currently are and what output you want when multiple modes exist. I won't bother otherwise though, it is a bit ugly, and I decided no need to make into a easy macro without further feedback. It can be a bit more flexible though than what David currently posted. I figured David would have posted a solution to figure this out already somewhere on the forum, but searching for mode on the forum is difficult. Andy |
Administrator
|
mea culpa ;-)
"David's solution only works if the variables are integer values and within a known integer value range.".... BUT: "realized this is probably a reasonable assumption on David's part (e.g. it works for likert responses, and when else is the modal value useful?) " ;-)) Vindicated HOWEVER:General is always nice. -- VARSTOCASES / MAKE X FROM V1 TO V4. **** adapt above line V1 TO V4 -> Your varlist***. AGGREGATE OUTFILE * / BREAK CASEID X / N=N. SORT CASES BY CASEID N (A) X (D). MATCH FILES / FILE * / BY CASEID / FIRST=@TOP@/LAST=@BOT@. COMPUTE UNIQUE=ANY(1,@BOT@ AND N > LAG(N),@BOT@ AND @TOP@). VECTOR MODE(4). **** adapt above line 4 -> Number of vars***. IF @BOT@ MODE(1)=X. DO REPEAT L=1 TO 3. **** adapt above line 3 -> Number of vars MINUS 1***. IF SUM(CASEID EQ LAG(CASEID,L),@BOT@, NOT UNIQUE, N=LAG(N,L)) EQ 4 MODE(L+1)=LAG(X,L). END REPEAT. EXE. SELECT IF @BOT@.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
In reply to this post by Bruce Weaver
Further to Bruce's post, as he states that there may be more than one mode and modes need not be of equal magnitude, it is perhaps advisable to produce a histogram to show the "humps".
HTH Robert |
Administrator
|
In reply to this post by Andy W
"I figured David would have posted a solution to figure this out already somewhere on the forum, but searching for mode on the forum is difficult."
Yeah, searching from Nabble is a PIA. This while much more complex than previous posts is more general than effort 1 but does not require V2C restructure/reshape. http://listserv.uga.edu/cgi-bin/wa?A2=ind1012&L=spssx-l&P=R26360&D=0&H=0&O=T&T=1
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
I'm out of the office until July 9th. Thank you. |
In reply to this post by David Marso
I think David should come with his own Acronym Glossary ha ha
"Lo there do I see my father. Lo there do I see my mother and my sisters and my brothers. Lo there do I see the line of my people, back to the beginning. Lo, they do call to me, they bid me take my place among them, in the Halls of Valhalla, where the brave may live...forever." |
Free forum by Nabble | Edit this page |