Sunday, October 28, 2012
Drawing Function vs Fitting Model in R
layout(matrix(c(1,2), 1,2, byrow=TRUE)) # http://www.stat.umn.edu/geyer/5102/examp/reg.html qftn <- function(x) x^2 * 3 curve(qftn, 1, 3, main = "Function: y = x^2 * 3") x <- c(1:3) y <- c(3, 12, 27) plot(x, y, main="Fitting model: y = x^2 * 3") model <- lm(y ~ I(x^2 * 3)) curve(predict(model, newdata = data.frame(x = x)), add = TRUE) # May also draw the coordinate pairs, like so: text(1,4, "(1,3)") text(2,13, "(2,12)") text(2.9,27, "(3,27)")