lecture_08

lecture_08 - } else { X = cbind(1,X) } betas = solve(t(X)%...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
BIOS 735: Statistical Computing Michael Wu Lecture 8: Function Examples September 15, 2011 Michael Wu (Lecture 8) BIOS 735 September 15, 2011 1 / 6
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Writing a Function When defining a funciton, there are three key components: the name, the arguments, and the body of the functon: <function name> = function(<list of arguments>) { <body> } functions are treated as variables (somehow) and their names follow the same rules Michael Wu (Lecture 8) BIOS 735 September 15, 2011 2 / 6
Background image of page 2
Writing a Function: Examples LinReg = function(y,X){ X = cbind(1,X) betas = solve(t(X)% * %X)% * %t(X)% * %y pred = X% * %betas resid = y - pred return(list(coef = betas, predicted = pred, residuals = resid)) } x = matrix(rnorm(500),ncol = 5) y = rnorm(100) + X% * %c(2.5,3,0,0,-4) LinReg(y,x) # lm(y˜x) LinReg(y, x[,1:2]) Michael Wu (Lecture 8) BIOS 735 September 15, 2011 3 / 6
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Writing a Function: Examples LinReg = function(y, X = NULL){ if (is.null(X)) { X = matrix(1, nrow = length(y))
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } else { X = cbind(1,X) } betas = solve(t(X)% * %X)% * %t(X)% * %y pred = X% * %betas resid = y - pred return(list(coef = betas, predicted = pred, residuals = resid)) } LinReg(y,x) LinReg(y) Michael Wu (Lecture 8) BIOS 735 September 15, 2011 4 / 6 Writing a Function: Examples LinReg = function(y, X = matrix(1, nrow = length(y))){ if (ncol(X) &gt;1) X = cbind(1,X) betas = solve(t(X)% * %X)% * %t(X)% * %y pred = X% * %betas resid = y - pred return(list(coef = betas, predicted = pred, residuals = resid)) } LinReg(y,x) LinReg(y) Michael Wu (Lecture 8) BIOS 735 September 15, 2011 5 / 6 Writing a Function: Examples LinPlot = function(y, X){ plot(y,X, . ..) } LinPlot(x[,1], y) LinPlot = function(y, X, . .. ){ plot(y,X, . ..) } LinPlot(x[,1], y, main = &quot;Eggplant&quot;) Michael Wu (Lecture 8) BIOS 735 September 15, 2011 6 / 6...
View Full Document

This note was uploaded on 11/18/2011 for the course BIOS 735 taught by Professor Staff during the Spring '10 term at UNC.

Page1 / 6

lecture_08 - } else { X = cbind(1,X) } betas = solve(t(X)%...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online