BookmarkSubscribeRSS Feed

[PROC IML과 R 패키지] SAS의 PROC IML에서 R 패키지를 사용하여서 데이터 처리 후 결과 값 반환 받기

Started ‎06-14-2020 by
Modified ‎06-14-2020 by
Views 179

***************************************************************;

* R 패키지로 처리 후 결과 값 반환 받기

***************************************************************; 

 

SAS에서 지원하지 않은 R 패키지의 다양한 함수를 SAS 의 Proc IML 내부에서 호출하여 사용이 가능하다.

단, 데이터 용량의 제한과 R 패키지에서 데이터 프레임과 Matrix 형태만 호출이 가능하다.

 

아마도 Proc IML에서 직접 R 패키지를 호출하여서 사용 시에는 여러 제약이 존재하겠지만, SAS 에서 R 패키지의 다양한 기능을 사용하여서 데이터 처리와 분석 영역의 확대가 가능하리라 생각이 된다.

 

****************************************************************;

* config 파일에 " -RLANG" 옵션 추가;

 

* R 패키 사용 가능 여부 확인 방법;

proc options option=RLANG;

run;

 

* SAS에서 R 패키지 사용을 위한 환경 처리 :http://support.sas.com/documentation/cdl/en/imlug/63541/HTML/default/viewer.htm#imlug_r_sect003.htm;

 

* SAS와 R 패키지 결과 전송은 R 패키지의 데이터 프레임과 Matrix 형태만 전송이 가능하다.

* 참고 : http://support.sas.com/documentation/cdl/en/imlug/63541/HTML/default/viewer.htm#imlug_r_sect007.htm;

 

 

* Calling Functions in the R Language

http://people.musc.edu/~elg26/teaching/statcomputing.2014/Lectures/Lecture3.SASIML/IML%20Lecture%20S...

 

 

* 다음은 binning을 위한 R 패키지 smbinning 과 binst 를 호출하여 처리하는 예제이다.;

***************************************************************;

* binary 타겟 데이터에 대한 binning;

* smbinning 예제;

* The main purpose of the package is to categorize a numeric variable into bins

* mapped to a binary target variable for its ulterior usage in scoring modeling;

***************************************************************;

proc iml;

  submit / R;

    # /* Package loading and data exploration */

    library(smbinning) # /* 사용 패키지 로딩 */

    data(chileancredit) # /* 분석을 위하여 사용 데이터 로딩 */

    chileancredit.train=subset(chileancredit,FlagSample==1)

    result=smbinning(df=chileancredit.train,y="FlagGB",x="TOB",p=0.05) # /* smbinning 실행 */

 

    # /* Generate new binned characteristic into a existing data frame */

    chileancredit= smbinning.gen(chileancredit,result,"gTOB") # /* 대상 데이터에 binning 결과 추가 */

    endsubmit;

    call ImportDataSetFromR("Work.MyData", "chileancredit"); # /* R 패키지의 데이터 프레임을 SAS 데이터로 전환 */

QUIT;

 

* sashelp.class;

* smbinning 패키지의 타겟은 Binary(0/1) 형식이어야 함.;

DATA CLASS;

 SET SASHELP.CLASS;

     IF sex = '남' THEN sex_tar = 0;

     ELSE sex_tar = 1;

RUN;

 

* 예제 데이터 생성;

DATA CLASS;

 SET CLASS CLASS CLASS CLASS CLASS CLASS CLASS CLASS CLASS;

RUN;

 

* 현재 x와 y는 R-project에서 integer 형식이어야 함.;

proc iml;

     run ExportDataSetToR("work.Class", "class" );  # /* SAS 데이터 세트를 R 패키지의 데이터 프레임으로 전환 */

     submit / R;

       print(class)

       print(str(class))

       # /* integer 형식으로 변경 */

       class$sex_tar <- as.integer(class$sex_tar)

       class$Weight <- as.integer(class$Weight)

       print(str(class))

 

       # /* Load package and its data */

       library(smbinning)

 

       # /* Run and save result */

       result=smbinning(df=class,y="sex_tar",x="Weight",p=0.05) 

       print(result)

       # /* Generate new binned characteristic into a existing data frame */

       chileancredit= smbinning.gen(class,result,"gTOB") # /* Update population */

     endsubmit;

     call ImportDataSetFromR("Work.MyData", "chileancredit");

QUIT;

 

***************************************************************;

* R Package 'binst'

* Data Preprocessing, Binning for Classification and Regression

* 참조 : https://github.com/Jules-and-Dave/binst;

***************************************************************;

* create_breaks(x, y = NULL, method = "kmeans", control = NULL, ...);

 

* A convenience functon for creating breaks with various methods;

* decisiontrees(dt,ctrees) : Create breaks using decision trees (recursive partitioning)

  create_dtbreaks(x, y, control = control) 

 

* earth(mars) : Create breaks using earth (i.e. MARS)

  create_earthbreaks(x, y, control = control)

 

* mdlp(entropy) : Create breaks using mdlp;

* create_mdlpbreaks(x, y);

 

* jenks : Create Jenks breaks

  create_jenksbreaks(x, control = control)

 

* kmeans : Create kmeans breaks.

create_kmeansbreaks(x, control = control);

 

DATA IRIS;

 SET SASHELP.IRIS;

     IF Species = 'Setosa'          THEN TARGET=1;

     ELSE IF Species = 'Versicolor' THEN TARGET=2;

     ELSE IF Species = 'Virginica'  THEN TARGET=3;

 

     if      _n_ <=10 then nom_var = '01';

     else if _n_ <=20 then nom_var = '02';

     else if _n_ <=30 then nom_var = '03';

     else if _n_ <=40 then nom_var = '04';

     else if _n_ <=50 then nom_var = '05';

     else if _n_ <=60 then nom_var = '06';

     else if _n_ <=70 then nom_var = '07';

     else if _n_ <=80 then nom_var = '08';

     else if _n_ <=90 then nom_var = '09';

     else if _n_ <=100 then nom_var = '10';

     else if _n_ <=110 then nom_var = '11';

     else if _n_ <=120 then nom_var = '12';

     else if _n_ <=130 then nom_var = '13';

     else if _n_ <=140 then nom_var = '14';

     else nom_var = '15';

RUN;

 

* 독립변수 : 연속형, 종속변수 : 명목형;

proc iml;

     run ExportDataSetToR("work.IRIS", "sasiris" );

     submit / R;

       # print(sasiris)

       print(str(sasiris))

       # /* Load package and its data */

       library(binst)

       library(earth)

       library(plotmo)

       library(plotrix)

       library(TeachingDemos)

       library(ape)

       library(BAMMtools)

       dt_breaks <- create_breaks(sasiris$SepalLength, sasiris$Species, method="mdlp")

       print(dt_breaks)

       sasiris$db_breaks <- create_bins(sasiris$SepalLength, dt_breaks)

     endsubmit;

     call ImportDataSetFromR("Work.iris_mdlp", "sasiris");

QUIT;

 

* 독립변수 : 연속형, 종속변수 : 연속형 -> earth;

proc iml;

     run ExportDataSetToR("work.iris", "sasiris" );

     submit / R;

       # print(sasiris)

       print(str(sasiris))

       # /* Load package and its data */

       library(binst)

       library(earth)

       library(plotmo)

       library(plotrix)

       library(TeachingDemos)

       library(ape)

       library(BAMMtools)

       dt_breaks <- create_breaks(sasiris$SepalWidth, sasiris$SepalLength, method="earth", control=list(glm=list(family=gaussian)))

       print(dt_breaks)

       sasiris$db_breaks <- create_bins(sasiris$SepalWidth, dt_breaks)

     endsubmit;

     call ImportDataSetFromR("Work.iris_earth", "sasiris");

QUIT;

Version history
Last update:
‎06-14-2020 10:44 PM
Updated by:
Contributors

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

Article Labels
Article Tags