Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. library("ggplot2", lib.loc="/usr/lib/R/library")
  2. library("boot", lib.loc="/usr/lib/R/library")
  3.  
  4. makePlots <- function(stddev){
  5. set.seed(1)
  6. x <- rnorm(100,mean=0,sd=1)
  7. h<-hist(x)
  8. xfit<-seq(min(x),max(x),length=100)
  9. yfit<-dnorm(xfit,mean=mean(x),sd=sd(x))
  10. yfit<-yfit*diff(h$mids[1:2])*length(x)
  11. lines(xfit,yfit,col="blue")
  12.  
  13. eps <- rnorm(100,mean=0,sd=stddev)
  14. h<-hist(eps)
  15. xfit<-seq(min(eps),max(eps),length=100)
  16. yfit<-dnorm(xfit,mean=mean(eps),sd=sd(eps))
  17. yfit<-yfit*diff(h$mids[1:2])*length(eps)
  18. lines(xfit,yfit,col="blue")
  19.  
  20. y <- -1+0.5*x + eps
  21. y
  22.  
  23. plot(x,y,main="X vs Y Scatter Plot")
  24.  
  25. model <- lm(y ~ x)
  26. print(summary(model))
  27. abline(model,col="red", lwd=10)
  28.  
  29. abline(c(-1,0.5),col="blue", lwd=5)
  30.  
  31. legend("bottomright",c("Least Squares","Population", "Polynomial"),lwd=c(2.5,2.5,2.5),col=c("red","blue","green"),lty=c(1,1,1))
  32.  
  33. quad <- lm(y ~ poly(x,2,raw=TRUE))
  34. quad
  35. fun <- function(x) quad$coefficients[3]*x^2+quad$coefficients[2]*x+quad$coefficients[1]
  36. curve(fun,col="green",add=TRUE, lwd=5)
  37.  
  38. rsq <- function(formula, data, indices){
  39. data <- data.frame(data)
  40. d <- data[indices,]
  41. fit <- lm(formula, data=d)
  42. return(summary(fit)$r.square)
  43. }
  44.  
  45. data <- data.frame(x,y)
  46.  
  47. results <- boot(data=data,statistic=rsq, R=1000, formula=y~x)
  48. print(results)
  49. plot(results)
  50.  
  51. return(model)
  52. }
  53. makePlots(0.25)
  54. makePlots(0.1)
  55. makePlots(0.5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement