SHARE
TWEET

Untitled

a guest Oct 13th, 2019 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Randomized the records in the data set and
  2. sz <- dim(faithful)[1]
  3. q <- order(runif(sz))
  4. r.data <- faithful[q, ]
  5.  
  6. #Generates the 80%train and 20%test sets
  7. train <- r.data[1:floor(0.80*sz), ]
  8. test <- r.data[floor(0.80*sz): (sz), ]
  9.  
  10. #Generates a Linear Regresion model from the train data
  11. model <- lm(waiting~eruptions, data = train)
  12.  
  13. summary(model)
  14. data.pred <- predict(model, test)
  15. cor(data.pred, test$waiting, method = "pearson")  
  16.  
  17. #dividing the dataset
  18. part1 <- faithful[1:floor(sz/2), ]
  19. part2 <- faithful[floor(sz/2):(sz), ]
  20. q.1 <- order(runif(dim(part1)[1]))
  21. q.2 <- order(runif(dim(part2)[1]))
  22. part1 <- part1[q.1, ]
  23. part2 <- part2[q.2, ]
  24.  
  25. part1.train <- part1[1:floor(0.80*dim(part1)[1]), ]
  26. part2.train <- part2[1:floor(0.80*dim(part1)[1]), ]
  27. test1 <- part1[floor(0.80*dim(part1)[1]): (dim(part1)[1]), ]
  28. test2 <- part2[floor(0.80*dim(part2)[1]): (dim(part2)[1]), ]
  29. part1.model <- lm(waiting~eruptions, data = part1.train)
  30. part2.model <- lm(waiting~eruptions, data = part2.train)
  31.  
  32. part1.pred <- predict(part1.model, test)
  33. part2.pred <- predict(part2.model, test)
  34.  
  35. cor(part1.pred, test$waiting, method = "pearson")
  36. cor(part2.pred, test$waiting, method = "pearson")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top