1. Project A and oral report tips. 2. Defining vectors and matrices within C. 3. C functions communicating. 4. Running C from terminal. Hw4 is up on the course website and is due Tues, Nov 15, 1030am. Late homeworks will not be accepted. When I say in 2d "sample 100 pairs of observations (Xi, Yi) with replacement," I mean, if your dataset has length n, then let b = sample(1:n, 100, rep=T) and for each element i in b, take (Xi, Yi). ETAS23 and ETAS24 contain two simulations so you can test out your maximum likelihood estimation routine for project B, if you like. They are on the course website. 1. Project A and oral report tips. For the oral projects. Rule 1: Do not look at me when you are talking! Rule 2: 6-7 minutes per oral report and everybody in the group must speak for roughly equal amounts. I will cut you off if your group goes over. You can have someone in the audience or on your team helping you with the time. Rule 3: Everyone must be respectful and quiet during other people's talks. No questions because we won't have time. Rule 4: Send me a pdf or powerpoint version of your slides the night before your talk, to frederic@stat.ucla.edu. That way, I can set up the talks in order ahead of time and we won't have to waste time in class waiting for each person to connect their laptop to the projector. About 6 or 7 slides seems right, though it's fine with me if you want fewer or more. Rule 5: Give us a sense of your data. Assume that the listener knows what the statistical methods you are using are. Tell us what they say about your data. Emphasize the results more than the methods. Go slowly in the beginning so that the listener really understands what your data are. Rule 6: Speculate and generalize but use careful language. Say "It seems" or "appears" rather than "is" when it comes to speculative statements or models. For example, you might say "The residuals appear approximately normal" or "a linear model seems to fit well" but not "The residuals are normal" or "The data come from a linear model". Rule 7: Start with an introduction explaining what your data are, how you got them, and why they are interesting (roughly 1-2 minutes), then show your results as clearly as possible, with figures preferred (roughly 2 minutes), and then conclude (roughly 1-2 minutes). In your conclusion, mention the limitations of your analysis and speculate about what might make a future analysis better, if you had infinite time. This might include collecting more data, or getting data on more variables, as well as more sophisticated statistical methods. For your written reports, rules 5-7 apply again. Replace "minutes" with "pages". Have just the text in the beginning, and then the figures at the end. 2. Vectors and matrices in C. You can just say double x[100] or double x[25][4], for example. In mypi2.c, /* Calculate the first 100 terms in the approximation of pi. */ #include #include void pi100 (double *y){ int i; double x[100]; x[0] = 1.0; y[0] = sqrt(6.0); for(i = 1; i < 100; i++) { x[i] = x[i-1] + pow(i+1.0,-2.0); y[i] = sqrt(6.0 * x[i]); } } In R, system("R CMD SHLIB mypi2.c") dyn.load("mypi2.so") a = .C("pi100",y=double(100)) plot(1:100,a$y,type="l",xlab="n",ylab="Approx. of pi") In mypi3.c, /* Calculate 1*2*3 + 4*5*6 + ... + 28*29*30. */ #include #include void pi100 (double *y){ int i,j; double x[10][3]; *y = 0.0; for(i = 0; i < 10; i++) { for(j = 0; j < 3; j++){ x[i][j] = 3*i + j + 1.0; } *y += x[i][0] * x[i][1] * x[i][2]; } } In R, system("R CMD SHLIB mypi3.c") dyn.load("mypi3.so") y = 1.0 a = .C("pi100",as.double(y)) a If you want y to be an n by m matrix, where n and m are inputs, you can just do, for instance, in mypi4.c, /* Calculate 1*2*3*...*m + (m+1)(m+2)...(2m) + ... + [(n-1)m+1][(n-1)m+2]...(nm). */ #include #include void pi102 (double *y, int *n, int *m){ int i,j; double x[*n][*m]; double a; /* First define the matrix. */ for(i = 0; i < *n; i++) { for(j = 0; j < *m; j++){ x[i][j] = *m*i + j + 1.0; } } /* Now multiply each row and add the product to y. */ *y = 0.0; for(i = 0; i < *n; i++){ a = 1.0; for(j = 0; j < *m; j++){ a *= x[i][j]; } *y += a; } } In R, system("R CMD SHLIB mypi4.c") dyn.load("mypi4.so") y = 1.0 a = .C("pi102",as.double(y),as.integer(10),as.integer(3)) a$y a = .C("pi102",y=as.double(y),as.integer(8),as.integer(4)) a$y Note that you have to say "y = as.double(y)" rather than just as.double(y) if you want to be able to call the result with a$y. To do this in R, you could do n = 8 m = 4 x = matrix(1:(n*m),byrow=T,ncol=m) y = apply(x,1,prod) sum(y) What if you want to define a vector of length n, or a matrix with a dimension n, and n is not an input or a known integer but instead something that must be calculated inside your function, or something that changes as you go through your function? Then you need to do dynamic memory allocation. We will discuss this next class. 3. Calling C functions from C. In mysd1.c, /* Create a 10 x 4 matrix of integers and compute the sample SD of each row. */ /* Row 1 will be (1, 4, 9, 16). Row 2 will be (25, 36, 49, 64), etc. */ #include #include double sd2(double *x, int n){ int i; double xbar, b; xbar = 0.0; for(i = 0; i < n; i++){ xbar += x[i]; } xbar = xbar/n; b = 0.0; // b will be the sum of squared deviations for(i = 0; i < n; i++){ b += pow(x[i] - xbar, 2); } return(pow(b/(n-1),0.5)); } void rowsd (double *y){ int i,j; double a; double x[10][4]; for(i = 0; i < 10; i++) { for(j = 0; j < 4; j++){ x[i][j] = pow(4*i + j + 1.0, 2); } y[i] = sd2(x[i], 4); } } In R, system("R CMD SHLIB mysd1.c") dyn.load("mysd1.so") a = .C("rowsd",y=double(10)) a$y ## to check it, sd(c(1,4,9,16)) sd(c(25,36,49,64)) 4. Running C from terminal. In mypi5.c, #include #include int main ( ){ int i; float x,y,h; printf("\n How many years old are you? "); scanf("%d", &i); printf("\n How long is the first side of your right triangle? "); scanf("%f", &x); printf("\n How long is the second side of your right triangle? "); scanf("%f", &y); h = sqrtf(x*x + y*y); printf("\n You are %d years old and the hypotenuse is %f . \n", i, h); return(i); } In terminal, gcc mypi5.c -o mypi5.out ./mypi5.out Coming up. ## Reading in from a file. ## Dynamic memory allocation. ## examples of C++. ## Making R packages. ## Generalized additive models.