Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.25 KB | None | 0 0
  1. data("ChickWeight")
  2. Dieta_1 = ChickWeight[ChickWeight$Diet==1,c('Time','weight')]
  3. Dieta_2 = ChickWeight[ChickWeight$Diet==2,c('Time','weight')]
  4. Dieta_3 = ChickWeight[ChickWeight$Diet==3,c('Time','weight')]
  5. Dieta_4 = ChickWeight[ChickWeight$Diet==4,c('Time','weight')]
  6.  
  7. #Esta funcion calcula el h optimo por GCV, y grafica las la regresion con sus
  8. #bandas de confianza.
  9. #Inputs:
  10. #vector y
  11. #vector x,
  12. #vector h: los distintos valores de h en donde evaluaremos
  13. #numeric deg: ??
  14. regressionPlot <- function(y, x, h, deg=1) {
  15.     alphamat = matrix(0,ncol=2,nrow=length(h))
  16.     alphamat[,2] = h
  17.     gcvs = gcvplot(y~x, alpha=alphamat, deg=deg,maxk=1000)
  18.    
  19.     #h optimo por GCV:
  20.     opth = max(gcvs$alpha[gcvs$values == min(gcvs$values),2])
  21.     locfitopt = locfit(y ~ x, alpha=c(0,opth),deg=1,maxk=1000)
  22.     # Bandas de confianza
  23.     plot(locfitopt, newdata=NULL, tr=NULL, what="coef", band="global", get.data=FALSE, col="red")
  24. }
  25.  
  26. par(mfrow=c(2,2)); #Que haga los 4 graficos en una misma ventana (grilla de 2x2)
  27. regressionPlot(Dieta_1$weight, Dieta_1$Time, seq(1,6,0.1));
  28. regressionPlot(Dieta_2$weight, Dieta_2$Time, seq(1,6,0.1));
  29. regressionPlot(Dieta_3$weight, Dieta_3$Time, seq(1,6,0.1));
  30. regressionPlot(Dieta_4$weight, Dieta_4$Time, seq(1,6,0.1));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement