How to get the R-square value in a MLR analysis using SVB? - Statistica Visual Basic - Statistica - Dell Community

How to get the R-square value in a MLR analysis using SVB?

How to get the R-square value in a MLR analysis using SVB?

This question is answered

Does anyone know how to get the R-square value in a MLR analysis using SVB?

I wrote a VB program for MLR analysis, but I don't know how to copy the R-square value from the Multiple Regression Results form using program.

For example, when I using program finished a MLR, there should be a Multiple Regression Results, incude r-square, adjust r2, p, var1, var2 and so on. if I want Msgbox a r2 result, how to implement?
 

thanks for your help!!

 

Robin

Verified Answer
  • Perhaps someone else can better answer this. nonlinear estimation is tricky because the output we need is only in the results window of the dialog and not accessible in a spreadsheet output. I am not sure how to grab pieces from that results window in SVB, but I do know a simple way to get R. R is simply the correlation between observed and predicted. So output the prediction output and use that as input in a correlation analysis to get R. here is some example code:

     

    '#Reference {C9E29002-3D45-11D4-9FF4-00C04FA0D540}#1.0#0#
    '#Reference {C9E2900D-3D45-11D4-9FF4-00C04FA0D540}#1.0#0#
    '#Reference {BE35B7EB-0D00-44D8-B3A5-0727DF5AD7C1}#1.0#0#
    '#Reference {C9E29001-3D45-11D4-9FF4-00C04FA0D540}#1.0#0#
    '#Language "WWB-COM"
    Option Base 1
    Sub Main

        Dim newanalysis As Analysis
        Set newanalysis = Analysis (scNonlinearEstimation, ActiveInputDataSet)
        Dim oStaDocs As StaDocuments

        ' Nonlinear Estimation:
        Dim oAD1 As STANonlinear.NlnStartup
        Set oAD1 = newanalysis.Dialog
        oAD1.NonlinearMethod = scNlnUserSpecifiedRegressionLeastSquares

        newanalysis.Run

        ' User-Specified Regression, Least Squares:
        Dim oAD2 As STANonlinear.NlnLSUserDefined
        Set oAD2 = newanalysis.Dialog
        oAD2.UserFunction = "v3=a +b*v4"
        oAD2.CasewiseDeletionOfMD = True

        newanalysis.Run

        ' Nonlinear Least Squares Model Estimation:
        Dim oAD3 As STANonlinear.NlnLSModelEstimation
        Set oAD3 = newanalysis.Dialog
        oAD3.EstimationMethod = scNlnLevenbergMarquardt
        oAD3.MaxNumberOfIterations = 50
        oAD3.ConvergenceCriterion = 6
        oAD3.StartValues = 0.1

        newanalysis.Run

        ' Results:
        Dim oAD4 As STANonlinear.NlnLSResults
        Set oAD4 = newanalysis.Dialog
        oAD4.AlphaForLimits = 95
        oAD4.PLevelForHighlighting = 0.05

    'create output of observed and predicted
        Dim PredOutput As Spreadsheet
        Set PredOutput = oAD4.PredictedValues.Item(1)


    'use prediction output as input in correlation matrix.
        Dim newanalysis2 As Analysis
        Set newanalysis2 = Analysis (scBasicStatistics, PredOutput)


        ' Product-Moment and Partial Correlations:
        Dim oAD6 As STABasicStatistics.BasStartup
        Set oAD6 = newanalysis2.Dialog
        oAD6.Statistics = scBasCorrelationMatrices

        newanalysis2.Run

        ' Product-Moment and Partial Correlations:
        Dim oAD7 As STABasicStatistics.BasCorrelation
        Set oAD7 = newanalysis2.Dialog
        oAD7.VariableList = "1-2"


        'Set oStaDocs = oAD7.Summary
        'newanalysis2.RouteOutput(oStaDocs).Visible = True
        'Set oStaDocs = Nothing
    Dim ModelRValue As Variant
    Dim CorrSS As Spreadsheet
    Set CorrSS = oAD7.Summary.Item(1)
    ModelRValue = CorrSS.Cells(1, 2)
    MsgBox "The nonlinear model R is: " & ModelRValue

    End Sub
     

All Replies
  • assuming you are using the Multiple Regression tool and not GLM....

    here is some SVB code that will use the active data set, let you select variables for regression and then output the R square of the model in a message box. I hope this helps.

    '#Language "WWB-COM"
    Option Base 1
    Sub Main

        Dim newanalysis As Analysis
        Set newanalysis = Analysis (scMultipleRegression, ActiveInputDataSet)
        Dim oStaDocs As StaDocuments

        ' Multiple Linear Regression: Adstudy.sta
        Dim oAD1 As STARegression.RegStartup
        Set oAD1 = newanalysis.Dialog
        oAD1.Variables = ""

        newanalysis.Run

        ' Multiple Regression Results: Adstudy.sta
        Dim oAD2 As STARegression.RegResults
        Set oAD2 = newanalysis.Dialog

        Dim Out1 As Spreadsheet
        Dim Rstat As Variant

        Set Out1= oAD2.Summary.Item(1)
        Set Rstat = Out1.Cells(2, 1)
        MsgBox "the model R Square is: " & Rstat

        Set oStaDocs = oAD2.Summary
        newanalysis.RouteOutput(oStaDocs).Visible = True
        Set oStaDocs = Nothing


    End Sub

  • Thank you so much!

    And there is another question: I want get the R value in the NonlinearEstimation, but the same method can only get the values in the summary form, not include the R value.

     

    '#Language "WWB-COM"
    Option Base 1
    Sub Main

     Dim newanalysis As Analysis
     Set newanalysis = Analysis (scNonlinearEstimation, ActiveInputDataSet)
     Dim oStaDocs As StaDocuments

     Dim oAD1 As STANonlinear.NlnStartup
     Set oAD1 = newanalysis.Dialog
     oAD1.NonlinearMethod = scNlnUserSpecifiedRegressionLeastSquares

     newanalysis.Run

     Dim oAD2 As STANonlinear.NlnLSUserDefined
     Set oAD2 = newanalysis.Dialog
     oAD2.UserFunction = "some linear functions"
     oAD2.CasewiseDeletionOfMD = True

     newanalysis.Run

     Dim oAD3 As STANonlinear.NlnLSModelEstimation
     Set oAD3 = newanalysis.Dialog
     oAD3.EstimationMethod = scNlnLevenbergMarquardt
     oAD3.MaxNumberOfIterations = 50
     oAD3.ConvergenceCriterion = 6
     oAD3.StartValues = 0.1

     newanalysis.Run


     Dim oAD4 As STANonlinear.NlnLSResults
     Set oAD4 = newanalysis.Dialog
     oAD4.AlphaForLimits = 95
     oAD4.PLevelForHighlighting = 0.05

     Dim Out1 As Spreadsheet
        Dim Rstat As Variant

        Set Out1= oAD4.Summary.Item(1)
        Set Rstat = Out1.Cells(1, 1)
        MsgBox Rstat


     Set oStaDocs = oAD4.Summary
     newanalysis.RouteOutput(oStaDocs).Visible = True
     Set oStaDocs = Nothing


    End Sub

     

    I can only get the values in the Nonlinear estimation results dialog. but how can I get the R in the Result form?


     Model is: v1=......
     Dependent variable: Var1               Independent variables: 5
     Loss function: least squares
     Final value: .01010969
     Proportion of variance accounted for: .83815015    R =.91550541

     

    Waiting for your reply.

    Thanks!

  • It seem that there is only one summary report form for nonlinear estimation, not inlude R value.

    But in the header of result form, it showed Final value, Proportion of variance accounted for:XXX and R value,

    How can I get them using SVB?

  • Perhaps someone else can better answer this. nonlinear estimation is tricky because the output we need is only in the results window of the dialog and not accessible in a spreadsheet output. I am not sure how to grab pieces from that results window in SVB, but I do know a simple way to get R. R is simply the correlation between observed and predicted. So output the prediction output and use that as input in a correlation analysis to get R. here is some example code:

     

    '#Reference {C9E29002-3D45-11D4-9FF4-00C04FA0D540}#1.0#0#
    '#Reference {C9E2900D-3D45-11D4-9FF4-00C04FA0D540}#1.0#0#
    '#Reference {BE35B7EB-0D00-44D8-B3A5-0727DF5AD7C1}#1.0#0#
    '#Reference {C9E29001-3D45-11D4-9FF4-00C04FA0D540}#1.0#0#
    '#Language "WWB-COM"
    Option Base 1
    Sub Main

        Dim newanalysis As Analysis
        Set newanalysis = Analysis (scNonlinearEstimation, ActiveInputDataSet)
        Dim oStaDocs As StaDocuments

        ' Nonlinear Estimation:
        Dim oAD1 As STANonlinear.NlnStartup
        Set oAD1 = newanalysis.Dialog
        oAD1.NonlinearMethod = scNlnUserSpecifiedRegressionLeastSquares

        newanalysis.Run

        ' User-Specified Regression, Least Squares:
        Dim oAD2 As STANonlinear.NlnLSUserDefined
        Set oAD2 = newanalysis.Dialog
        oAD2.UserFunction = "v3=a +b*v4"
        oAD2.CasewiseDeletionOfMD = True

        newanalysis.Run

        ' Nonlinear Least Squares Model Estimation:
        Dim oAD3 As STANonlinear.NlnLSModelEstimation
        Set oAD3 = newanalysis.Dialog
        oAD3.EstimationMethod = scNlnLevenbergMarquardt
        oAD3.MaxNumberOfIterations = 50
        oAD3.ConvergenceCriterion = 6
        oAD3.StartValues = 0.1

        newanalysis.Run

        ' Results:
        Dim oAD4 As STANonlinear.NlnLSResults
        Set oAD4 = newanalysis.Dialog
        oAD4.AlphaForLimits = 95
        oAD4.PLevelForHighlighting = 0.05

    'create output of observed and predicted
        Dim PredOutput As Spreadsheet
        Set PredOutput = oAD4.PredictedValues.Item(1)


    'use prediction output as input in correlation matrix.
        Dim newanalysis2 As Analysis
        Set newanalysis2 = Analysis (scBasicStatistics, PredOutput)


        ' Product-Moment and Partial Correlations:
        Dim oAD6 As STABasicStatistics.BasStartup
        Set oAD6 = newanalysis2.Dialog
        oAD6.Statistics = scBasCorrelationMatrices

        newanalysis2.Run

        ' Product-Moment and Partial Correlations:
        Dim oAD7 As STABasicStatistics.BasCorrelation
        Set oAD7 = newanalysis2.Dialog
        oAD7.VariableList = "1-2"


        'Set oStaDocs = oAD7.Summary
        'newanalysis2.RouteOutput(oStaDocs).Visible = True
        'Set oStaDocs = Nothing
    Dim ModelRValue As Variant
    Dim CorrSS As Spreadsheet
    Set CorrSS = oAD7.Summary.Item(1)
    ModelRValue = CorrSS.Cells(1, 2)
    MsgBox "The nonlinear model R is: " & ModelRValue

    End Sub
     

  • Thank you for your help!

    Although the two result of R have a little difference, but it is in the acceptable range
    . I can use it for batch work.

    When you know how to grab pieces from that results window in SVB, please email me and let me know: yhz-163@163.com.

    Thank you again!