Created
November 3, 2013 00:44
-
-
Save baogorek/7285186 to your computer and use it in GitHub Desktop.
Nonlinear least squares example. Notice the "normal equations" on line 10. Three things are different than the linear case: 1) The X matrix is now the derivative of the mean function with respect to beta 2) We're solving for the change in beta from the last iteration 3) We have to iterate See http://en.wikipedia.org/wiki/Non-linear_least_squares …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
N <- 10000 | |
x <- rnorm(N) | |
y <- exp(-.75 + 1.2*x) + rnorm(N) | |
alpha <- -.5 | |
beta <- 1 | |
for ( i in 1:10) { # 10 iterations, for no particular reason | |
X <- cbind(exp(alpha + beta*x), x*exp(alpha + beta*x)) | |
pred <- exp(alpha + beta*x) | |
diff.soln <- solve(t(X) %*% X) %*% t(X) %*% (y - pred) | |
alpha <- alpha + diff.soln[1] | |
beta <- beta + diff.soln[2] | |
cat("iteration: ", i, ", alpha: ", alpha, ", beta: ", beta, "\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment