R – first steps

I talked a little while ago about downloading R. Time to update you on progress since I ran through some of the sample code in the Introduction to R.

Well, I couldn’t quite believe how simple the syntax is, it feels quite strange using <- all over the place, and not a proc in sight (R exposes functionality through functions).

Moving very slightly forwards from the intro code I wrote, with a bit of trial and error, the following:


library("foreign")
testsas <- read.ssd("c:/temp", "test", sascmd="c:/program files/sas institute/sas/v8/sas.exe")
attach(testsas)
summary(testsas)
fm<-lm(y~x,testsas)
summary(fm)
plot(x,y)
abline(coef(fm))

And it worked!

The "exciting" thing about this rather mundane code is that the read.ssd() function is reading a SAS® dataset into R! Reading a SAS dataset is no more difficult than loading the "foreign" library, and then passing the path, the dataset name (without the .sas7bdat extension), and the path to sas.exe.

The dataset is fairly dull - it has two variables x and y, where y is x plus x times a random amount between 0 and 1.

The attach() function makes the dataset available as a data frame, the summary() function gives basic summary stats, the lm() function is equivalent to proc reg, and stores the regression coefficients to a new variable structure fm.

reg

Another call to fm() gives me linear regression coefficients (the m and c, from y = mx + c) and goodness of fit statistics.

Finally the calls to plot() and abline() plot a chart of observations, overlayed with a line of best fit.

Not very interesting but: (a) reading SAS data into R is quick and easy; (b) writing R code is quick and easy; (c) the results from the R code were verified by those produced in SAS.

Point (c) isn't much of a surprise - but the painlessness of both (a) and (b) is!

I look forward with great interest to hear what comes out of the SAS Global Forum in a few days time!

Technorati Tags: , ,

This entry was posted in R-Project, SAS and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>