2.16.1 Examples: Perform Logistic regression in R by using LabTalk

Logistic regression models a relationship between predictor variables and a categorical response variable. The following example shows how to perform a Logistic regression on data Origin2016\Samples\Statistics\LogRegData.dat by using Labtalk R object and RS objectin Script window.

We first read data into Origin worksheet, and send the data as R data frame. Then perform Logistic Regression on imported data by using R command glm, finally create a new Origin worksheet as the summary table for fitting results, and send the results including parameters, residual, aic and dispersion...calculated by R into summary table worksheet.

R Object

//R object example to call R in LabTalk
//run.section(testRex,LogisticRoex)

//Import sample data
newbook;
string fn = system.path.program$ + "Samples\Statistics\LogRegData.dat"; 
impasc fname:=fn$ options.sparklines:=0;

//Check whether R is installed
if( R.Init()<0 )
{
	type -b "Please install R software first.";
	return;
}

//Send imported data in Origin to R's data frame variable dfy
//Data frame dfy includes four columns: Age, Salary, Gender and Career_Change
//Gender and Career_Change columns are category data
R.Send("!", dfy, 2);

//Perform Logistic Regression on imported data
//Results are stored in R's list variable yr
R.Exec( "yf<-glm( Career_Change ~ Age + Salary + Gender, family=binomial(logit), data=dfy)" );
R.Exec("yr<-summary(yf)");

//New a workbook to output R's result
newbook;
page.longname$ = "Logistic Regression Result";

//Send R's coefficients matrix in list object to Origin's worksheet 
//and start at 1st column in the worksheet
R.Receive("1", yr$coefficients);

int nc = wks.ncols;
wks.ncols = nc + 6;

//Calculate the exp of parameters
wcol( nc+1 )[L]$ = "Exp of Parameter";
wcol( nc+1 ) = exp( wcol(2) );

//Set label and column Long Name for calculated result
wcol( nc+2 )[1]$ = "Residual";

wcol( nc+3 )[L]$ = "DF";
wcol( nc+4 )[L]$ = "Deviance";
wcol( nc+5 )[L]$ = "AIC";
wcol( nc+6 )[L]$ = "Dispersion Parameter for Binomial Family";

//Send R's residual info to Origin's worksheet
double dfr, devr, aic, rlev;
//Get LabTalk double variables from list object's element variables in R
R.GetReal( dfr, yr$df.residual );
R.GetReal( devr, yr$deviance );
R.GetReal( aic, yr$aic );
R.GetReal( rlev, yr$dispersion );

//Set worksheet's cells with LabTalk double variables
wcol( nc+3 )[1] = dfr;
wcol( nc+4 )[1] = devr;

wcol( nc+5 )[1] = aic;
wcol( nc+6 )[1] = rlev;

//Clear all R variables
R.Reset();

RS Object

You need to Setup R Serve before running this example.

//RS example to call Rserve in LabTalk	
//Import sample data
newbook;
string fn = system.path.program$ + "Samples\Statistics\LogRegData.dat"; 
impasc fname:=fn$ options.sparklines:=0;

//Connect Rserve. An Rserve should be set up first.
if( RS.Init( ***.***.***.***, 12306 )<0 ) //input the IP address of the server side here
{
	type -b "Fail to connect R server.";
	return;
}

//Send imported data in Origin to R's data frame variable dfy
//Data frame dfy includes four columns: Age, Salary, Gender and Career_Change
//Gender and Career_Change columns are category data
RS.Send("!", dfy, 2);

//Perform Logistic Regression on imported data
//Results are stored in R's list variable yr
RS.Exec( "yf<-glm( Career_Change ~ Age + Salary + Gender, family=binomial(logit), data=dfy)" );
RS.Exec("yr<-summary(yf)");

//New a workbook to output R's result
newbook;
page.longname$ = "Logistic Regression Result";

//Send R's coefficients matrix in list object to Origin's worksheet 
//and start at 1st column in the worksheet
RS.Receive("1", yr$coefficients);

int nc = wks.ncols;
wks.ncols = nc + 6;

//Calculate the exp of parameters
wcol( nc+1 )[L]$ = "Exp of Parameter";
wcol( nc+1 ) = exp( wcol(2) );

//Set label and column Long Name for calculated result
wcol( nc+2 )[1]$ = "Residual";

wcol( nc+3 )[L]$ = "DF";
wcol( nc+4 )[L]$ = "Deviance";
wcol( nc+5 )[L]$ = "AIC";
wcol( nc+6 )[L]$ = "Dispersion Parameter for Binomial Family";

//Send R's residual info to Origin's worksheet
double dfr, devr, aic, rlev;
//Get LabTalk double variables from list object's element variables in R
RS.GetReal( dfr, yr$df.residual );
RS.GetReal( devr, yr$deviance );
RS.GetReal( aic, yr$aic );
RS.GetReal( rlev, yr$dispersion );

//Set worksheet's cells with LabTalk double variables
wcol( nc+3 )[1] = dfr;
wcol( nc+4 )[1] = devr;

wcol( nc+5 )[1] = aic;
wcol( nc+6 )[1] = rlev;

//Clear all R variables
RS.Reset();