Programmability in SPSS 14, SPSS 15 and SPSS 16 The Revolution Continues Jon Peck Technical Advisor SPSS Copyright (c) SPSS Inc, 2007

  • View

  • Download

Embed Size (px)

Text of Programmability in SPSS 14, SPSS 15 and SPSS 16 The Revolution Continues Jon Peck Technical Advisor...

  • Slide 1

Programmability in SPSS 14, SPSS 15 and SPSS 16 The Revolution Continues Jon Peck Technical Advisor SPSS Copyright (c) SPSS Inc, 2007 Slide 2 Recap of SPSS 14 Python programmability Developer Central New features in SPSS 15 programmability Writing first-class procedures Updating the data New features in SPSS 16 programmability Interacting with the user Q & A Conclusion Copyright (c) SPSS Inc, 2007 Agenda Slide 3 "Because of programmability, SPSS 14 is the most important release since I started using SPSS fifteen years ago." "I think I am going to like using Python." "Python and SPSS 14 and later are, IMHO, GREAT!" "By the way, Python is a great addition to SPSS." From InfoWorld (April 19, 2007) "Of all the tools fueling the dynamic-language trend in the enterprise, general-purpose dynamic languages such as Python and Ruby present the greatest upside for enhancing developer productivity." Copyright (c) SPSS Inc, 2007 Quotations from SPSS Users Slide 4 SPSS provides a powerful engine for statistical and graphical methods and for data management. Python provides a powerful, elegant, and easy-to-learn language for controlling and responding to this engine. Together they provide a comprehensive system for serious applications of analytical methods to data. Copyright (c) SPSS Inc, 2007 The Combination of SPSS and Python Slide 5 SPSS 14.0 provided Programmability Multiple datasets Variable and File Attributes Programmability read-access to case data Ability to control SPSS from a Python program SPSS 15 adds Read and write case data Create new variables directly rather than generating syntax Create pivot tables and text blocks via backend API's Easier setup SPSS 16 will add EXTENSION command for user procedures with SPSS syntax Dataset features for complex data management Ability to use R procedures within SPSS through R Plug-In Copyright (c) SPSS Inc, 2007 Programmability Features in SPSS 14, 15, and 16 Slide 6 Makes possible easy jobs that respond to datasets, output, environment Allows greater generality, more automation Makes jobs more robust Allows extending the capabilities of SPSS Enables better organized and more maintainable code Facilitates staff specialization Increases productivity More fun Copyright (c) SPSS Inc, 2007 Programmability Advantages Slide 7 Python extends SPSS via General programming language Access to variable dictionary, case data, and output Access to standard and third-party modules SPSS Developer Central modules Module structure for building libraries of code Runs in "back-end" syntax context (like macro) SaxBasic scripting runs in "front-end" context Two modes Traditional SPSS syntax window Drive SPSS from Python (external mode) Optional install (licensed with SPSS Base) Copyright (c) SPSS Inc, 2007 Programmability Overview Slide 8 SPSS is not the owner or licensor of the Python software. Any user of Python must agree to the terms of the Python license agreement located on the Python web site. SPSS is not making any statement about the quality of the Python program. SPSS fully disclaims all liability associated with your use of the Python program. Copyright (c) SPSS Inc, 2007 Legal Notice Slide 9 Supports implementing various programming languages Requires a programmer to implement a new language VB.NET Plug-In available on Developer Central Works only in external mode Copyright (c) SPSS Inc, 2007 The SPSS Programmability Software Development Kit Slide 10 Python interpreter embedded within SPSS SPSS runs in traditional way until BEGIN PROGRAM command is found Python collects commands until END PROGRAM command is found; then runs the program Python can communicate with SPSS through API's (calls to functions) Includes running SPSS syntax inside Python program Includes creating macro values for later use in syntax Python can access SPSS output and data OMS is a key tool Copyright (c) SPSS Inc, 2007 How Programmability Works Slide 11 BEGIN PROGRAM. import spss, spssaux spssaux.GetSPSSInstallDir("SPSSDIR") spssaux.OpenDataFile("SPSSDIR/employee data.sav") # find categorical variables catVars = spssaux.VariableDict(variableLevel=['nominal', 'ordinal']) if catVars: spss.Submit("FREQ " + " ".join(catVars.variables)) # create a macro listing categorical variables spss.SetMacroValue("!catVars", " ".join(catVars.variables)) END PROGRAM. DESC !catVars. Run Copyright (c) SPSS Inc, 2007 Example: Summarize Categorical Variables Slide 12 Two modes of operation SPSS Drives mode (inside): traditional syntax context BEGIN PROGRAM program END PROGRAM Program in 14, 15, or 16 is in Python or, new in 16, in R X Drives mode (outside): eXternal program drives SPSS Python interpreter (or VB.NET) No SPSS Viewer, Data Editor, or SPSS user interface Output sent as text to the application can be suppressed Has performance advantages Build programs with an IDE Even if to be run in traditional mode Copyright (c) SPSS Inc, 2007 Programmability Inside or Outside SPSS Slide 13 Copyright (c) SPSS Inc, 2007 PythonWin IDE Controlling SPSS (eXternal Mode) Slide 14 Be productive quickly Get more return as you learn more Python Tutorial Cheeseshop over 2200 packages as of April 11, 2007 SPSS Developer Central SPSS Programming and Data Management, 4th ed, 2006. SPSS Programming and Data Management Copyright (c) SPSS Inc, 2007 Python Resources Slide 15 Dive Into Python book or PDF Dive Into Python Practical Python by Magnus Lie Hetland Extensive examples and discussion of Python Python Cookbook, 2 nd ed by Martelli, Ravenscroft, & Ascher Python in a Nutshell, 2 nd ed by Martelli, O'Reilly Very clear, comprehensive reference material wxPython in Action by Rappin and Dunn Explains user interface building with wxPython Copyright (c) SPSS Inc, 2007 Python Books Slide 16 scipy 0.5.2 Scientific Algorithms Library for Python scipy is an open source library of scientific tools for Python. scipy gathers a variety of high level science and engineering modules together as a single package. scipy provides modules for statistics, optimization, integration, linear algebra, Fourier transforms, signal and image processing, genetic algorithms, ODE solvers, special functions, and more. scipy requires and supplements NumPy, which provides a multidimensional array object and other basic functionality. Python is becoming a major language for scientific computing Copyright (c) SPSS Inc, 2007 Cheeseshop: scipy Slide 17 SPSS Developer Central is the web home for developing SPSS applications SPSS Developer Central Python,.NET, R Integration Plug-Ins Supplementary modules by SPSS and others Articles on programmability and graphics Forums for asking questions and exchanging information Programmability Extension SDK Get Python itself from or SPSS 14, 15 use 2.4. (2.4.3) SPSS 16 will use 2.5 Not limited to programmability GPL graphics User-contributed code Key Supplementary Modules spssaux spssdata New for SPSS 15 trans extendedTransforms rake pls enhanced Copyright (c) SPSS Inc, 2007 SPSS Developer Central Slide 18 module on Developer Central can merge two tables into one. E.g., Ctables significance tests into main tables Merge or replace cells with cells from a different table Flexibly define the join can also censor cells, e.g., blank statistics based on small counts. Merge example: data on importance of education qualifications for immigration by region of Europe CTABLES /TABLE qfimeduBin BY Region /TITLES TITLE='Qualifications for Immigration' /COMPARETEST TYPE=PROP Copyright (c) SPSS Inc, 2007 Example: Manipulating Output: Merging Tables Slide 19 Copyright (c) SPSS Inc, 2007 Ctables Output Slide 20 BEGIN PROGRAM. import spss, tables cmd=r"""CTABLES /TABLE qfimeduBin BY Region /TITLES TITLE='Qualifications for Immigration' /COMPARETEST TYPE=PROP""" tables.mergeLatest(cmd, autofit=False) END PROGRAM. Runs Ctables and merges test table into main table Using default merge behavior "If it really is this simple this will generate a lot of excitement for us." "This is really fantastic." Copyright (c) SPSS Inc, 2007 Program to Merge Slide 21 Copyright (c) SPSS Inc, 2007 Merged Output Slide 22 You can extend SPSS capabilities by building new procedures Or use ones that others have built Combine SPSS procedures and transformations with Python logic Poisson regression (SPSS 14) example using iterated CNLR New raking procedure built over GENLOG GENLIN in SPSS 15 Calculate data aggregates in SPSS and pass to algorithm coded in Python Raking procedure starts with AGGREGATE; uses GENLOG Acquire case data and compute in Python Use Python standard modules and third-party additions Partial Least Squares Regression (pls module) Copyright (c) SPSS Inc, 2007 Approaches to Creating New Procedures Slide 23 Common to adapt existing libraries or code for use as Python extension modules C, C++, VB, Fortran,... Python tools and API's to assist Chap 25 in Python in a Nutshell Tutorial on extending and embedding the Python interpreter Tutorial on extending and embedding the Python interpreter Call R programs with SPSS 16 Copyright (c) SPSS Inc, 2007 Adapt Existing Code Libraries Slide 24 Regression with large number of predictors (even k > N) Similar to Principal Components but considers dependent variable simultaneously Calculates principal components of (y, X) then use regression on the scores instead of original data Equivalent to ordinary regression when number of factors equals number of predictors and one y variable For more information see An Optimization Perspective on Kernel Partial Least Squares Regression.pdf.An Optimization Perspective on Kernel Partial Least Squares Regression.pdf Copyright (c) SPSS Inc, 2007 Partial Least Squares Regression Slide 25 Strategy Fetches data from SPSS Uses scipy matrix operations to compute results Third-party module from Cheeseshop Writes pivot tables to SPSS Viewer Subject to OMS SPSS 14 viewer