1. Final projects. 2. R Cookbook ch. 11. 3. XCode. 4. Compiling C and calling C from R. 5. Hello world. 6. pi. 7. dnorm. 8. Sum of squared differences between observations. 1. Final projects. I made a couple small changes to the schedule. It only affects teams 5 and 8. Here is the schedule now. Tue, Nov 26 [1] DAS GUPTA, APARUPA . MAO, JUNHUA . WANG, PENG . [2] CAI, YUAN . SINGH, VIKRAM VIR . WU, GUANI . [3] LIU, YUNLEI . KARWA, ROHAN SUNIL . KUHFELD, MEGAN REBECCA . [4] MURPHY, JOHN LEE . HONG, JI YEON . SHAH, VAIBHAV CHETAN . Tue, Dec 3 [5] Csapo, Marika . LEWIS, BRONWYN ASHLEIGH . ZHANG, QIANG . [6] NAN, XIAOMENG . ARELLANO, RYAN CHRISTOPHER . SHU, LE . [7] WANG, XINYUE . CHEN, JIAMIN . HAN, TIAN . [8] THAKUR, MANOJ RAMESHCHANDRA . POWELL, CHRISTOPHER GRANT . [9] XIE, MEIHUI . WONG, ALEX KING LAP . XIE, DAN . [10] TAN, MENGXIN . ROBERTS, SHANE WILLIAM STROUTH . HUANG, PENG JUN . [11] YU, CHENGCHENG . WU, JOHN SHING . MOUSAVI, SEYED ALI . Thu, Dec 5 [12] FERNANDEZ, KEITH EDWARD . AHLUWALIA, ANSUYA . HAROUNIAN, BENJAMIN . [13] GU, JIAYING . RAMIREZ, ARTURO . DEMIRDJIAN, LEVON . [14] HUYNH, HIEN TRINH . SHEN, LINLING . CHANDRASEKHARAN, MANJANA . [15] WISNER, TRISTAN GARDNER . MAGYAR, ZSUZSANNA BLANKA . HE, JIA . [16] KRUMHOLZ, SAMUEL DAVID . KATTE, SAMIR RAJENDRA . WAINFAN, KATHRYN TANYA . [17] WANG, TENG . SCHWEIG, JONATHAN DAVID . Gupta, Hitesh . 2. R Cookbook ch11. After today we will have covered all of ch. 1-11, and next week I'll discuss ch. 13.1 and 13.2, but that's probably it for R Cookbook. The rest will be C. lm, p269-285. lm(y ~ x), y = b0 + b1 x + eps lm(y ~ u + v + w) fits y = b0 + b1 u + b2 v + b3 w + eps. lm(y ~ u + v + w + 0) for no intercept, b0. lm(y ~ u*v*w) for all interactions. See p279. y = b0 + b1u + b2v + b3w + b4uv + b5uw + b6vw + b7 uvw + e. To specify a particular interaction, use :. lm(y ~ u + v + w + u:v:w) for just the uvw term. See p280. This fits y = b0 + b1 u + b2 v + b3 w + b4 uvw + eps. p280, lm(y ~ (u+v+w)^i) for all order i interactions. u = rnorm(10000) v = rnorm(10000) w = rnorm(10000) x = rnorm(10000) y = 10 + 20*u + 30*u*v + rnorm(10000,sd=.01) j = lm(y ~ (u + v + w + x)^3) summary(j) Use - to eliminate a term. j = lm(y ~ (u + v + w + x)^3 - v:w:x - v) summary(j) step, p281, to do stepwise regression, forward or backward. k = step(j,direction="backward") AIC = Akaike's Information Criterion, lowest AIC is best fitting model. Assuming eps = iid Normal(mean 0, some variance sigma^2), can calculate the likelihood associated with a particular model and parameters. AIC = -2 log (likelihood) + 2p. Lower AIC is preferred. For comparing nested models, the difference in AIC is generally approximately chi square distributed. summary(k) k2 = step(lm(y ~ 1), direction="forward", scope = ( ~ (u+v+w+x)^4), trace=0) summary(k2) p285, I(u+v) for a term that actually just represents u+v. Similarly, I(u^2) for a term that is u^2, or I(u*v) for a column that is u*v. k = lm(y ~ I(u*v*w)) k = lm(y ~ I(x^2 + x)) p292, confint for confidence intervals. confint(k2) p293, plot the residuals with plot(k2, which=1), and other plots with plot(k2). 3. XCode, for Mac OSX, includes a C and C++ compiler, among other tools. You can get XCode from http://wildfire.stat.ucla.edu/rick/xcode . Click on the one choice, xcode_3.2.6_and_ios, to get it. I will only leave it up there for a week or so. It takes a couple hours to download. If you have a PC, you will need a compiler. There are many free ones. See for instance http://www.compilers.net/dir/free/compilers/ccpp.htm . 4. Compiling C and calling C from R. The first step to writing C code is opening a text editor. Write your C code, call it something.c, then compile it, to create an object file called something.so. Then you can load that into R. It seems to primarily work with R from terminal, but you can run C in R in terminal, do save.image(), and then start R, use the same working directory, and then do load(".RData") . 5. Hello world. Create a C function to print "Hello world!" and call this function n times in R. 4a. In a text editor, create a C file. Say it's called hello.c The file looks like: #include #include /* Start a comment. Continue your comment. */ void hello (int *n) { int i,j; double a2; a2 = 4.5; j = 3; for(i = 0; i < *n; i++) Rprintf("The %d rd integral is %f .\n", j, a2); } 5b. In UNIX, in same directory where hello.c is, type R CMD SHLIB hello.c or, in R, do system("R CMD SHLIB hello.c") 5c. In R, in the same directory, do dyn.load("hello.so") hello2 <- function(n){ .C("hello",as.integer(n)) } y = hello2(10)