Guest User

Untitled

a guest
Feb 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. # create the true relationship
  2. f <- function(x) x^2 # true model
  3. x <- seq(0, 1, by = 0.01)
  4. y <- f(x)
  5.  
  6. # plot the true function
  7. plot(x, y, type = "l", col = "red", ylim = c(-0.2, 1.2), lwd = 4)
  8.  
  9. # fit 100 models
  10. set.seed(1)
  11. for (i in 1:100)
  12. {
  13. errors <- rnorm(n, 0, sigma) # random errors, have standard deviation sigma
  14. obs_y <- f(obs_x) + errors # observed y = true_model + error
  15. model <- lm(obs_y ~ obs_x) # fit a linear model to the observed values
  16. points(obs_x[i], mean(obs_y[i]), col = "green") # mean values
  17. abline(model, col = "purple") # plot the fitted model
  18. }
Add Comment
Please, Sign In to add comment