Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. x1 <- as.numeric(round(runif(10,-40,40),2))
  2. x2 <- as.numeric(round(x1*1.4+60,2))
  3. x3 <- as.numeric(round(runif(10,20,60),2))
  4. x4 <- as.numeric(round(x2*0.9+60,2))
  5. x5 <- as.numeric(round(x2*x3*0.9+60,2))
  6. x6 <- as.numeric(round(x2*x3*x4*x5/1000000,2))
  7.  
  8. y <- as.numeric(round(runif(10,50,150),2))
  9.  
  10. df <- data.frame(y,x1,x2,x3,x4,x5,x6)
  11.  
  12.  
  13. library(pls)
  14. # plsr, RMSEP
  15. mod.plsr <- plsr(y~x1+x2+x3+x4+x5+x6, data=df,
  16. ncomp=5, validation="CV")
  17.  
  18. ## delta vector contains RMSEP differences
  19. err.CV = c()
  20. for (i in 1:10) {err.CV[i] = RMSEP(mod.plsr)$val[i*2+1]}
  21. delta = err.CV[1:9] - err.CV[2:10]
  22. comp.plsr = min(which(delta<0.05))
  23. plot(RMSEP(mod.plsr),legendpos="topright", main="")
  24.  
  25. ## mixed model regression coefficients
  26. mod.plsr.opt = plsr(y~x1+x2+x3+x4+x5+x6, data=df,
  27. ncomp = comp.plsr)
  28.  
  29. coef(mod.plsr.opt)
  30.  
  31. , , 1 comps
  32.  
  33. y
  34. x1 4.324635e-05
  35. x2 6.054166e-05
  36. x3 3.218208e-05
  37. x4 5.449111e-05
  38. x5 4.142277e-03
  39. x6 4.653091e-03
  40.  
  41.  
  42.  
  43.  
  44. library(plsRglm)
  45. # plsrglm, BIC
  46. mod.plsrglm = plsRglm(y~x1+x2+x3+x4+x5+x6, data=df,
  47. nt=5, model="pls")
  48.  
  49. # use BIC to determine optimal number of components
  50. comp.plsrglm = which(mod.plsrglm$InfCrit[,2] == min(mod.plsrglm$InfCrit[,2]))-1
  51.  
  52. # refit model and extract beta coefficients from the optimal model
  53. mod.plsrglm.opt = plsRglm(y~x1+x2+x3+x4+x5+x6, data=df,
  54. nt=comp.plsr, model="pls")
  55.  
  56. mod.plsrglm.opt$Coeffs
  57.  
  58. [,1]
  59. Intercept -4.422569e+05
  60. x1 -3.150225e+03
  61. x2 -2.355536e+03
  62. x3 4.523422e+00
  63. x4 5.120661e+03
  64. x5 -1.490321e-01
  65. x6 7.920704e-02
  66.  
  67. coef(mod.plsr.opt, intercept = TRUE)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement