Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #Second assignment
  2. #2.1
  3. #Target = blood.Systolic, Weight & height are variables.
  4. data <- as.data.frame(read.csv("Women.csv", sep = ";"))
  5.  
  6. #fit=glm(formula = Blood.systolic~., data = data)
  7.  
  8. logit <- function(y, x, start = NULL, weights = NULL, offset = NULL, ...) {
  9. # m = glm(y ~ 0 + x^2, family = gaussian, start = start, ...)
  10. xy=cbind(x[,-1],y)
  11. x=x[,-1]
  12. data=as.data.frame(xy)
  13. ret=lm(y~.^2, data=data)
  14. return(ret)
  15. }
  16. #Wrongly followed the vignette instruction at slide 7.
  17. #Did not understand that model generated false solutions as the results of our plots very reasonable.
  18. #Also the warning message did not seem too unreasonable.
  19. #When investigating the solution noticed that the first column of x should be extracted.
  20.  
  21.  
  22. library(grid)
  23. library(libcoin)
  24. library(mvtnorm)
  25. library(partykit)
  26. library(sandwich)
  27.  
  28.  
  29. model = mob(Blood.systolic~.|height+weight, data = data, fit = logit, control = mob_control(minsize = 5000))
  30.  
  31. plot(model)
  32.  
  33.  
  34. sortedHeights = sort(data$height)
  35. sortedWeights = sort(data$weight)
  36.  
  37. height = seq(sortedHeights[1],sortedHeights[length(sortedHeights)],length.out = 100)
  38. weight = seq(sortedWeights[1],sortedWeights[length(sortedWeights)],length.out = 100)
  39.  
  40.  
  41. test = expand.grid(height, weight)
  42.  
  43.  
  44.  
  45. colnames(test) = c("height","weight")
  46.  
  47. predictions = predict(model, newdata = test, type="response")
  48.  
  49. #Edited after hand-in (COMPLETION)
  50. #Changed from type link to type response
  51.  
  52.  
  53.  
  54. library(ggplot2)
  55. library(gridExtra)
  56. ggplot(test, aes(height,weight, fill = predictions)) + geom_raster()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement