'Begin Description 'This script plots the normal-, binomial- and chisquare probability distributions; 'the user chooses the number of degrees of freedom etc.. Charttype and titles 'can be customized from within the dialogbox. The script does a fair amount of 'checking of the input (range and sign of values), currently no checking is done 'against requesting plots with more categories than SPSS' graph-engine will accept; '3000. ' 'Written 27/09/1998 by Rolf Kjшller (rolf.kjoeller@get2net.dk) . 'End Description Option Explicit 'For localized versions of Windows which use comma (,) 'instead of period (.) as the decimal separator, 'change the following from False to True Const DECIMAL_COMMA As Boolean = False Global strMacrocall As String Global booInputIsOK As Boolean Global strMainTitle As String Global strSubTitle As String Global intChartType As Integer Sub Main Dim blnOverwriteWarning As Boolean If SPSSVersion8 Then 'don't warn unless the data have been modified blnOverwriteWarning = objSpssApp.Documents.GetDataDoc(0).Modified Else 'or install code from query.sbs to check blnOverwriteWarning = True End If If blnOverwriteWarning Then If vbCancel = MsgBox ("Current data will be overwritten! OK to proceed?", _ vbExclamation + vbOkCancel) Then Exit Sub End If End If Call MainDialog End Sub Sub MainDialog Dim distributions$(3) distributions$(1)="Normal" distributions$(2)="Binomial" distributions$(3)="Chisquare" Dim stepvalues$(6) stepvalues$(1)="1" stepvalues$(2)="0.5" stepvalues$(3)="0.1" stepvalues$(4)="0.05" stepvalues$(5)="0.01" stepvalues$(6)="0.005" Begin Dialog UserDialog 420,140,"Plot Probability Distributions",.RunMainDialog Text 30,7,220,21,"Probability Distribution:",.maintext OKButton 290,20,100,20,.cmdOK CancelButton 290,50,100,20,.cmdCancel DropListBox 30,20,225,77,distributions(),.distribution Text 30,50,140,21,"Mean:",.meantext TextBox 210,50,45,20,.mean Text 30,75,140,20,"Standard Deviation:",.stdevtext TextBox 210,75,45,20,.stdev Text 30,100,140,21,"Step Value:",.stepvaluetext ListBox 185,100,70,28,stepvalues(),.stepvalue Text 30,50,140,21,"n trials:",.ntext TextBox 210,50,45,20,.n Text 30,75,140,20,"Prob:",.Probtext TextBox 210,75,45,20,.Prob Text 30,50,140,20,"Degrees of Freedom:",.dfstext TextBox 210,50,45,20,.dfs PushButton 290,100,100,20,"Options ...",.cmdOptions End Dialog Dim MainDlg As UserDialog Dialog MainDlg End Sub Sub OptionsDialog Begin Dialog UserDialog 420,140,"Plot Probability Distributions: Options",.RunOptionsDialog Text 30,7,140,20,"Titles:",.title PushButton 290,20,100,20,"Continue",.cmdContinue TextBox 30,20,230,20,.MainTitle TextBox 30,50,230,20,.SubTitle OptionGroup.ChartType OptionButton 30,100,60,20,"Line",.OptionButton1 OptionButton 120,100,60,20,"Area",.OptionButton2 OptionButton 210,100,60,20,"Bar",.OptionButton3 Text 30,87,90,14,"Graph:",.Text1 End Dialog Dim OptionsDlg As UserDialog Dialog OptionsDlg End Sub Private Function RunMainDialog(DlgItem As String, Action As Integer, SuppValue As Integer) As Boolean Select Case Action Case 1 'Dialog box initialization DlgVisible "meantext",True DlgVisible "mean",True DlgVisible "stdevtext",True DlgVisible "stdev",True DlgVisible "stepvaluetext",True DlgVisible "stepvalue",True DlgVisible "dfstext",False DlgVisible "dfs",False DlgVisible "ntext",False DlgVisible "n",False DlgVisible "Probtext",False DlgVisible "Prob",False DlgValue "stepvalue",2 DlgFocus "distribution" strMainTitle="Normal Distribution" strSubTitle="Mean=" + DlgText("mean") + ", Standard Deviation=" + DlgText("stdev") intChartType=0 Case 2 'Value changing or button pressed Select Case dlgItem Case "distribution" Select Case SuppValue Case 0 DlgVisible "meantext",True DlgVisible "mean",True DlgVisible "stdevtext",True DlgVisible "stdev",True DlgVisible "ntext",False DlgVisible "n",False DlgVisible "Probtext",False DlgVisible "Prob",False DlgVisible "dfstext",False DlgVisible "dfs",False DlgVisible "stepvaluetext",True DlgVisible "stepvalue",True strMainTitle="Normal Distribution" strSubTitle="Mean=" + DlgText("mean") + ", Standard Deviation=" + DlgText("stdev") intChartType=0 Case 1 DlgVisible "meantext",False DlgVisible "mean",False DlgVisible "stdevtext",False DlgVisible "stdev",False DlgVisible "ntext",True DlgVisible "n",True DlgVisible "Probtext",True DlgVisible "Prob",True DlgVisible "dfstext",False DlgVisible "dfs",False DlgVisible "stepvaluetext",False DlgVisible "stepvalue",False strMainTitle="Binomial Distribution" strSubTitle="n=" + DlgText("n") + ", Prob=" + DlgText("Prob") intChartType=2 Case 2 DlgVisible "meantext",False DlgVisible "mean",False DlgVisible "stdevtext",False DlgVisible "stdev",False DlgVisible "ntext",False DlgVisible "n",False DlgVisible "Probtext",False DlgVisible "Prob",False DlgVisible "dfstext",True DlgVisible "dfs",True DlgVisible "stepvaluetext",True DlgVisible "stepvalue",True strMainTitle="Chisquare Distribution" strSubTitle="df=" + DlgText("dfs") intChartType=0 End Select Case "cmdOK" strMainTitle="'" + strMainTitle + "'" strSubTitle="'" + strSubTitle + "'" Select Case DlgText("distribution") Case "Normal" Call CheckInput (DlgText("mean"), DlgText("stdev")) If booInputIsOK Then Call BuildMacroCall (DlgText("mean"), DlgText("stdev")) Call plot RunMainDialog = False Else RunMainDialog = True End If Case "Binomial" Call CheckInput (DlgText("n"), DlgText("Prob")) If booInputIsOK Then Call BuildMacroCall (DlgText("n"), DlgText("Prob")) Call plot RunMainDialog = False Else RunMainDialog = True End If Case "Chisquare" Call CheckInput (DlgText("dfs")) If booInputIsOK Then Call BuildMacroCall (DlgText("dfs")) Call plot RunMainDialog = False Else RunMainDialog = True End If End Select Case "cmdCancel" End Case "cmdOptions" Call OptionsDialog RunMainDialog = True End Select Case 3 'TextBox or ComboBox text changed Select Case DlgItem Case "mean" strMainTitle="Normal Distribution" strSubTitle="Mean=" + DlgText("mean") + ", Standard Deviation=" + DlgText("stdev") intChartType=0 Case "stdev" strMainTitle="Normal Distribution" strSubTitle="Mean=" + DlgText("mean") + ", Standard Deviation=" + DlgText("stdev") intChartType=0 Case "n" strMainTitle="Binomial Distribution" strSubTitle="n=" + DlgText("n") + ", Prob=" + DlgText("Prob") intChartType=2 Case "Prob" strMainTitle="Binomial Distribution" strSubTitle="n=" + DlgText("n") + ", Prob=" + DlgText("Prob") intChartType=2 Case "dfs" strMainTitle="Chisquare Distribution" strSubTitle="df=" + DlgText("dfs") intChartType=0 End Select Case 4 'Focus changed Case 5 'Idle End Select End Function Private Function RunOptionsDialog(DlgItem As String, Action As Integer, SuppValue As Integer) As Boolean Select Case Action Case 1 'Dialog box initialization DlgText "MainTitle", strMainTitle DlgText "SubTitle", strSubTitle DlgValue "ChartType", intChartType Case 2 'Value changing or button pressed Select Case dlgItem Case "cmdContinue" strMainTitle=DlgText("MainTitle") strSubTitle=DlgText("SubTitle") intChartType=DlgValue("ChartType") Exit Function End Select Case 3 'TextBox or ComboBox text changed Case 4 'Focus changed Case 5 'Idle End Select End Function Sub CheckInput (ParamArray InputValueList()) Dim i As Integer Dim j As Integer booInputIsOK=False For j=0 To UBound(Inputvaluelist) '*************************************** 'First check: Is the inputvalue numeric? '*************************************** If DECIMAL_COMMA Then 'dots are replaced with commas: For i=1 To Len(InputValuelist(j)) If Mid(InputValuelist(j),i,1) = "." Then InputValuelist(j)=Left(InputValuelist(j),i-1)+","+Right(InputValuelist(j),Len(InputValuelist(j))-i) Next i End If booInputIsOK=IsNumeric(InputValuelist(j)) If Not booInputIsOK Then MsgBox "An input field is either empty or contains non-numeric or multiple values" Exit Sub End If '**************************************************** 'Second check: Is the inputvalue within proper range? '**************************************************** Select Case DlgText("distribution") Case "Normal" If j=1 Then booInputIsOK = (CDbl(InputValuelist(j))>0) If Not booInputIsOK Then MsgBox "Standard Deviation must be greater than 0" Exit Sub End If End If Case "Binomial" Select Case j Case 0 booInputIsOK = (CDbl(InputValuelist(j))>0 And CDbl(InputValuelist(j))=Int(CDbl(InputValuelist(j)))) If Not booInputIsOK Then MsgBox "n must be an integer greater than 0" Exit Sub End If Case 1 booInputIsOK = (CDbl(InputValuelist(j))>=0 And CDbl(InputValuelist(j))<=1) If Not booInputIsOK Then MsgBox "Prob must be a number between 0 and 1" Exit Sub End If End Select Case "Chisquare" booInputIsOK = (CDbl(InputValuelist(j))>0 And CDbl(InputValuelist(j))=Int(CDbl(InputValuelist(j)))) If Not booInputIsOK Then MsgBox "Degrees of Freedom must be an integer greater than 0" Exit Sub End If End Select Next j End Sub Sub BuildMacroCall(ParamArray InputValueList()) Dim i As Integer Dim j As Integer For j=0 To UBound(Inputvaluelist) 'commas are replaced with dots: For i=1 To Len(InputValuelist(j)) If Mid(InputValuelist(j),i,1) = "," Then InputValuelist(j)=Left(InputValuelist(j),i-1)+"."+Right(InputValuelist(j),Len(InputValuelist(j))-i) Next i Next j Select Case DlgText("distribution") Case "Normal" strMacrocall="!norm mean=" + InputValuelist(0) + " stdev=" + InputValuelist(1) + " step=" + DlgText("stepvalue") + " ." Case "Binomial" strMacrocall="!bin n=" + InputValuelist(0) + " p=" + InputValuelist(1) + " ." Case "Chisquare" strMacrocall="!chisq df=" + InputValuelist(0) + " step=" + DlgText("stepvalue") + " ." End Select 'MsgBox strMacrocall End Sub Sub plot Dim strCommand As String strCommand = strCommand + "SET ERRORS=OFF ." &vbCrLf strCommand = strCommand + "/******************************************************************************" &vbCrLf strCommand = strCommand + "/* Normal:" &vbCrLf strCommand = strCommand + "/******************************************************************************" &vbCrLf strCommand = strCommand + "DEFINE !norm (mean=!TOKENS(1) /stdev=!TOKENS(1) /step=!DEFAULT(0.05) !TOKENS(1)) ." &vbCrLf strCommand = strCommand + "NEW FILE ." &vbCrLf strCommand = strCommand + "INPUT PROGRAM ." &vbCrLf strCommand = strCommand + "LOOP x=(-4*!stdev+!mean) TO (4*!stdev+!mean) BY !step ." &vbCrLf strCommand = strCommand + "COMPUTE cpx=CDF.NORM(x,!mean,!stdev) ." &vbCrLf strCommand = strCommand + "COMPUTE px=CDF.NORM(x,!mean,!stdev)-CDF.NORM(LAG(x),!mean,!stdev) ." &vbCrLf strCommand = strCommand + "IF x EQ (-4*!stdev+!mean) px=cpx ." &vbCrLf strCommand = strCommand + "END CASE ." &vbCrLf strCommand = strCommand + "END LOOP ." &vbCrLf strCommand = strCommand + "END FILE ." &vbCrLf strCommand = strCommand + "END INPUT PROGRAM ." &vbCrLf strCommand = strCommand + "EXECUTE ." &vbCrLf strCommand = strCommand + "VARIABLE LABELS x 'Z' px 'p(z=Z)' cpx 'p(z<=Z)' ." &vbCrLf strCommand = strCommand + "!ENDDEFINE ." &vbCrLf strCommand = strCommand + "/******************************************************************************" &vbCrLf strCommand = strCommand + "/* Binomial:" &vbCrLf strCommand = strCommand + "/******************************************************************************" &vbCrLf strCommand = strCommand + "DEFINE !bin (n=!TOKENS(1) /p=!TOKENS(1)) ." &vbCrLf strCommand = strCommand + "NEW FILE ." &vbCrLf strCommand = strCommand + "INPUT PROGRAM ." &vbCrLf strCommand = strCommand + "LOOP x=0 TO !n BY 1 ." &vbCrLf strCommand = strCommand + "COMPUTE cpx=CDF.BINOM(x,!n,!p) ." &vbCrLf strCommand = strCommand + "COMPUTE px=CDF.BINOM(x,!n,!p)-CDF.BINOM(LAG(x),!n,!p) ." &vbCrLf strCommand = strCommand + "IF x EQ 0 px=cpx ." &vbCrLf strCommand = strCommand + "END CASE ." &vbCrLf strCommand = strCommand + "END LOOP ." &vbCrLf strCommand = strCommand + "END FILE ." &vbCrLf strCommand = strCommand + "END INPUT PROGRAM ." &vbCrLf strCommand = strCommand + "EXECUTE ." &vbCrLf strCommand = strCommand + "FORMAT x (F4) ." &vbCrLf strCommand = strCommand + "VARIABLE LABELS x 'X' px 'p(x=X)' cpx 'p(x<=X)' ." &vbCrLf strCommand = strCommand + "!ENDDEFINE ." &vbCrLf strCommand = strCommand + "/******************************************************************************" &vbCrLf strCommand = strCommand + "/* Chisquare:" &vbCrLf strCommand = strCommand + "/******************************************************************************" &vbCrLf strCommand = strCommand + "DEFINE !chisq (df=!TOKENS(1) /step=!TOKENS(1)) ." &vbCrLf strCommand = strCommand + "NEW FILE ." &vbCrLf strCommand = strCommand + "INPUT PROGRAM ." &vbCrLf strCommand = strCommand + "LOOP x=0 TO 5*!df BY !step ." &vbCrLf strCommand = strCommand + "COMPUTE cpx=CDF.CHISQ(x,!df) ." &vbCrLf strCommand = strCommand + "COMPUTE px=CDF.CHISQ(x,!df)-CDF.CHISQ(LAG(x),!df) ." &vbCrLf strCommand = strCommand + "IF x EQ 0 px=cpx ." &vbCrLf strCommand = strCommand + "DO IF cpx GT 0.9999 ." &vbCrLf strCommand = strCommand + "BREAK ." &vbCrLf strCommand = strCommand + "END IF ." &vbCrLf strCommand = strCommand + "END CASE ." &vbCrLf strCommand = strCommand + "END LOOP ." &vbCrLf strCommand = strCommand + "END FILE ." &vbCrLf strCommand = strCommand + "END INPUT PROGRAM ." &vbCrLf strCommand = strCommand + "EXECUTE ." &vbCrLf strCommand = strCommand + "VARIABLE LABELS x 'Q' cpx 'p(q<=Q)' px 'p(q=Q)' ." &vbCrLf strCommand = strCommand + "!ENDDEFINE ." &vbCrLf strCommand = strCommand + "/******************************************************************************" &vbCrLf strCommand = strCommand + "/* Plot:" &vbCrLf strCommand = strCommand + "/******************************************************************************" &vbCrLf strCommand = strCommand + "DEFINE !plot (type=!TOKENS(1)) ." &vbCrLf strCommand = strCommand + "!IF (!type !EQ 0) !THEN ." &vbCrLf strCommand = strCommand + "GRAPH" &vbCrLf strCommand = strCommand + "/TITLE=" + strMainTitle &vbCrLf strCommand = strCommand + "/SUBTITLE=" + strSubTitle &vbCrLf strCommand = strCommand + "/LINE(SIMPLE)=VALUE(cpx) BY x ." &vbCrLf strCommand = strCommand + "GRAPH" &vbCrLf strCommand = strCommand + "/TITLE=" + strMainTitle &vbCrLf strCommand = strCommand + "/SUBTITLE=" + strSubTitle &vbCrLf strCommand = strCommand + "/LINE(SIMPLE)=VALUE(px) BY x ." &vbCrLf strCommand = strCommand + "!IFEND ." &vbCrLf strCommand = strCommand + "!IF (!type !EQ 1) !THEN ." &vbCrLf strCommand = strCommand + "GRAPH" &vbCrLf strCommand = strCommand + "/TITLE=" + strMainTitle &vbCrLf strCommand = strCommand + "/SUBTITLE=" + strSubTitle &vbCrLf strCommand = strCommand + "/LINE(AREA)=VALUE(cpx) BY x ." &vbCrLf strCommand = strCommand + "GRAPH" &vbCrLf strCommand = strCommand + "/TITLE=" + strMainTitle &vbCrLf strCommand = strCommand + "/SUBTITLE=" + strSubTitle &vbCrLf strCommand = strCommand + "/LINE(AREA)=VALUE(px) BY x ." &vbCrLf strCommand = strCommand + "!IFEND ." &vbCrLf strCommand = strCommand + "!IF (!type !EQ 2) !THEN ." &vbCrLf strCommand = strCommand + "GRAPH" &vbCrLf strCommand = strCommand + "/TITLE=" + strMainTitle &vbCrLf strCommand = strCommand + "/SUBTITLE=" + strSubTitle &vbCrLf strCommand = strCommand + "/BAR(SIMPLE)=VALUE(cpx) BY x ." &vbCrLf strCommand = strCommand + "GRAPH" &vbCrLf strCommand = strCommand + "/TITLE=" + strMainTitle &vbCrLf strCommand = strCommand + "/SUBTITLE=" + strSubTitle &vbCrLf strCommand = strCommand + "/BAR(SIMPLE)=VALUE(px) BY x ." &vbCrLf strCommand = strCommand + "!IFEND ." &vbCrLf strCommand = strCommand + "!ENDDEFINE ." &vbCrLf strCommand = strCommand + "SET ERRORS=LISTING ." &vbCrLf strCommand = strCommand + "/******************************************************************************" &vbCrLf strCommand = strCommand + strMacrocall &vbCrLf strCommand = strCommand + "!plot type=" + Str(intChartType) &vbCrLf objSpssApp.ExecuteCommands strCommand , False End Sub Function SPSSVersion8 As Boolean 'turn on error handling On Error Resume Next 'this will cause an error in SPSS 7.5, specifically: ' Error 10091 ' OLE Automation: no such property or method. objSpssApp.ScriptParameter(0) SPSSVersion8 = (Err = 0) 'clear the error, if any Err.Clear 'turn off error handling On Error GoTo 0 End Function