Guest User

Untitled

a guest
May 26th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 KB | None | 0 0
  1. #########################
  2. #Entendendo um GAMLSS####
  3. #########################
  4. library(gamlss)
  5. library(gamlss.cens)
  6. library(gamlss.util)
  7. library(gamlss.dist)
  8.  
  9. ### Exemplo 1 #####
  10. set.seed(30)
  11. femea<-rnorm(60,40,10)
  12. set.seed(30)
  13. macho<-rnorm(60,80,10)
  14. peso<-c(femea,macho)
  15. genero<- c(rep('femea',60), rep('macho',60))
  16.  
  17. dados<-data.frame(peso,genero)
  18. head(dados)
  19.  
  20. #LM
  21. model1=lm(peso~genero)
  22. summary(model1)
  23.  
  24. #GAMLSS
  25. model2<-gamlss(peso~genero)
  26. summary(model2)
  27. exp(2.39510)
  28.  
  29.  
  30.  
  31. #obtendo os parametros estimados
  32. dados$genero[1]
  33. model2$mu.fv[1]
  34. model2$sigma.fv[1]
  35.  
  36. dados$genero[100]
  37. model2$mu.fv[100]
  38. model2$sigma.fv[100]
  39.  
  40. cbind(genero,media=model2$mu.fv,desvio.p=model2$sigma.fv)
  41.  
  42.  
  43. hist(peso[genero=='femea'],probability = T)
  44. curve(dnorm(x,model1$coefficients[1],11.06),add=T,col=2,lwd=4)#lm
  45. curve(dnorm(x,model2$mu.fv[1],model2$sigma.fv[1]),add=T,col=4,lwd=4)#gamlss
  46.  
  47. hist(peso[genero=='macho'],probability = T)
  48. curve(dnorm(x,sum(model1$coefficients),11.06),add=T,col=2,lwd=4)#lm
  49. curve(dnorm(x,model2$mu.fv[120],model2$sigma.fv[120]),add=T,col=4,lwd=4)#gamlss
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. #Exemplo 2 - Variancia diferentes #####
  57. set.seed(30)
  58. femea<-rnorm(60,40,10)
  59. set.seed(30)
  60. macho<-rnorm(60,80,20)
  61. peso<-c(femea,macho)
  62. dados<-data.frame(peso,genero)
  63. head(dados)
  64.  
  65.  
  66. #LM
  67. model1=lm(peso~genero)
  68. summary(model1)
  69.  
  70. #GAMLSS
  71. model2<-gamlss(peso~genero,sigma.formula =~genero,data=dados)
  72. summary(model2)
  73.  
  74. cbind(genero,media=model2$mu.fv,desvio.p=model2$sigma.fv)
  75.  
  76.  
  77. hist(peso[genero=='femea'],probability = T)
  78. curve(dnorm(x,model1$coefficients[1],17.49),add=T,col=2,lwd=4)#lm
  79. curve(dnorm(x,model2$mu.fv[1],model2$sigma.fv[1]),add=T,col=4,lwd=4)#gamlss
  80.  
  81. hist(peso[genero=='macho'],probability = T,ylim=c(0,0.026))
  82. curve(dnorm(x,sum(model1$coefficients),17.49),add=T,col=2,lwd=4)#lm
  83. curve(dnorm(x,model2$mu.fv[120],model2$sigma.fv[120]),add=T,col=4,lwd=4)#gamlss
  84.  
  85.  
  86.  
  87. summary(model1)
  88. #SD = 2.25; 3.19
  89. summary(model2)
  90. #sd= 1.416 ; 3.16
  91.  
  92.  
  93.  
  94.  
  95. #predicoes
  96. model2$mu.fv
  97. model2$sigma.fv
  98. model2$sigma.fv^2
  99.  
  100.  
  101. dados_novo<-data.frame(genero=c('macho','femea'))
  102.  
  103. predito<-predictAll(model2,newdata=dados_novo, type='response')
  104. predito$mu
  105. predito$sigma
  106. predito$sigma^2
  107.  
  108.  
  109.  
  110.  
  111.  
  112. ### Exemplo 3- Survival ####
  113. library(gamlss.cens)
  114.  
  115.  
  116. set.seed(10)
  117. homem<-rWEI(300,2,2)
  118. set.seed(10)
  119. mulher<-rWEI(300,4,5)
  120. hist(homem)
  121. hist(mulher)
  122.  
  123. genero=c(rep('H',300),rep("M",300))
  124. tempos=c(homem,mulher)
  125. set.seed(20)
  126. delta=rbinom(600,1,0.9)
  127. dados<-data.frame(tempos,delta,genero)
  128. head(dados)
  129.  
  130. km=survfit(Surv(tempos,delta)~genero)
  131. plot(km,col=c(3,6),lwd=4)
  132.  
  133. #forma~escala~1
  134. model1=gamlss(Surv(tempos,delta)~1,family = cens(WEI))
  135. summary(model1)
  136. curve(1-pWEI(x,model1$mu.fv[1],model1$sigma.fv[1]),add=T,lwd=3)
  137.  
  138. #forma~genero escala~1
  139. model2=gamlss(Surv(tempos,delta)~genero,family = cens(WEI))
  140. summary(model2)
  141. curve(1-pWEI(x,model2$mu.fv[1],model2$sigma.fv[1]),add=T,lwd=2,lty=2)
  142. curve(1-pWEI(x,model2$mu.fv[600],model2$sigma.fv[600]),add=T,lwd=2,lty=2)
  143.  
  144.  
  145.  
  146. #forma~escala~genero
  147. model3=gamlss(Surv(tempos,delta)~genero,sigma.fo=~genero,family = cens(WEI),data=dados)
  148. summary(model3)
  149. curve(1-pWEI(x,model3$mu.fv[1],model3$sigma.fv[1]),add=T,lwd=2,col=1)
  150. curve(1-pWEI(x,model3$mu.fv[600],model3$sigma.fv[600]),add=T,lwd=2,col=1)
  151.  
  152.  
  153. wp(model1,ylim.all = 1.5)
  154. wp(model2,ylim.all = 1.5)
  155. wp(model3,ylim.all = 1.5)
  156.  
  157.  
  158. #### metodo de seleção
  159. m0=gamlss(Surv(tempos,delta)~1,family = cens(WEI))
  160. m1<-stepGAICAll.A(m0,scope=list(lower=~1,upper=~genero))
  161. summary(m1)
  162.  
  163.  
  164. #predicao
  165. gen.cens(WEI)
  166. dados_novo=data.frame(genero=c("H","M"))
  167. predito<-predictAll(model3,newdata=dados_novo, type='response')
  168. predito$mu
  169. predito$sigma
  170.  
  171. #E(Y)
  172. predito$mu*gamma(1/predito$sigma+1)
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. #### Exemplo 4 - non-lienar####
  183. library(gamlss)
  184. library(gamlss.util)
  185. set.seed(1)
  186. x1=runif(200,0,10)
  187. plot(x1,6*sin(x1*pi/4),ylab='mu')
  188. set.seed(1)
  189. y=6*sin(x1*pi/4)+rnorm(200)
  190.  
  191. m1<-gamlss(y~x1) #mu linear
  192. par(mfrow=c(1,2))
  193. plot(x1,6*sin(x1*pi/4),ylab='mu')
  194. term.plot(m1,parameter = 'mu')
  195. par(mfrow=c(1,1))
  196. wp(m1)
  197.  
  198.  
  199. m2<-gamlss(y~pb(x1)) #mu nao linear
  200. edfAll(m2) #gl>2 indica nao linear
  201. par(mfrow=c(1,2))
  202. plot(x1,6*sin(x1*pi/4),ylab='mu')
  203. term.plot(m2,parameter = 'mu')
  204. par(mfrow=c(1,1))
  205. wp(m2)
  206.  
  207.  
  208. AIC(m1,m2)
  209. BIC(m1,m2)
  210.  
  211. #valores esperados
  212. plot(x1,y)
  213. lines(x1[order(x1)],m1$mu.fv[order(x1)],col=1,lwd=3)
  214. lines(x1[order(x1)],m2$mu.fv[order(x1)],col=2,lwd=3)
  215.  
  216.  
  217. dados=data.frame(y,x1)
  218.  
  219. #densidades por pontos de x1
  220. plotSimpleGamlss(y,x1, model=m2, data=dados,
  221. x.val=seq(1,10,1),val=1, N=1000)
  222.  
  223. #percentis
  224. centiles(m2, xvar=x1, lwd=2,cent=c(5,50,95))
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231. #### Exemplo 5 - non-lienar#########
  232.  
  233. set.seed(1)
  234. x1=runif(200,0,10)
  235. plot(x1,6*sin(x1*pi/4),ylab='mu')
  236. plot(x1,0.5*x1,ylab='sigma')
  237.  
  238. set.seed(1)
  239. y=6*sin(x1*pi/4)+0.5*x1*rnorm(200)
  240.  
  241.  
  242. #mu nao linear e sigma constante
  243. m2<-gamlss(y~pb(x1))
  244. edfAll(m2) #gl>2 indica nao linear
  245. term.plot(m2,parameter = 'mu')
  246. wp(m2)
  247.  
  248. #mu nao linear e sigma linear
  249. m3<-gamlss(y~pb(x1),sigma.fo=~x1)
  250. edfAll(m3) #gl>2 indica nao linear
  251. par(mfrow=c(2,2))
  252. plot(x1,6*sin(x1*pi/4),ylab='mu')
  253. term.plot(m3,parameter = 'mu')
  254. plot(x1,0.5*x1,ylab='sigma')
  255. term.plot(m3,parameter = 'sigma')
  256. par(mfrow=c(1,1))
  257. wp(m3)
  258.  
  259.  
  260. AIC(m2,m3)
  261. BIC(m2,m3)
  262.  
  263. #valores esperados
  264. plot(x1,y)
  265. #variancia constante
  266. lines(x1[order(x1)],m2$mu.fv[order(x1)],col=1,lwd=3)
  267. #variancia~x1(tempo)
  268. lines(x1[order(x1)],m3$mu.fv[order(x1)],col=2,lwd=3)
  269.  
  270.  
  271. dados=data.frame(y,x1)
  272.  
  273. par(mfrow=c(1,2))
  274.  
  275. #variancia constante
  276. plotSimpleGamlss(y,x1, model=m2, data=dados,
  277. x.val=seq(1,10,1),val=2, N=1000)
  278.  
  279. #variancia~X1
  280. plotSimpleGamlss(y,x1, model=m3, data=dados,
  281. x.val=seq(1,10,1),val=2, N=1000)
  282.  
  283. #percentis
  284.  
  285. #variancia constante
  286. centiles(m2, xvar=x1, lwd=2,cent=c(5,50,95))
  287. #variancia~X1
  288. centiles(m3, xvar=x1, lwd=2,cent=c(5,50,95))
  289. par(mfrow=c(1,1))
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299. #### Exemplo 6 - non-lienar#######
  300.  
  301. set.seed(1)
  302. x1=runif(200,0,10)
  303. par(mfrow=c(1,2))
  304. plot(x1,6*sin(x1*pi/4),ylab='mu')
  305. plot(x1,5*sin(x1*pi/6)+6,ylab='sigma')
  306. par(mfrow=c(1,1))
  307.  
  308. set.seed(1)
  309. y=6*sin(x1*pi/4)+(5*sin(x1*pi/6)+6)*rnorm(200)
  310.  
  311. #media nao linear e sigma constante
  312. m2<-gamlss(y~pb(x1))
  313. edfAll(m2) #gl>2 indica nao linear
  314. term.plot(m2,parameter = 'mu')
  315. wp(m2)
  316.  
  317. #media nao linear e sigma linear
  318. m3<-gamlss(y~pb(x1),sigma.fo=~x1)
  319. edfAll(m3) #2 indica ñ linear
  320. par(mfrow=c(2,2))
  321. plot(x1,6*sin(x1*pi/4),ylab='mu')
  322. term.plot(m3,parameter = 'mu')
  323. plot(x1,5*sin(x1*pi/6)+6,ylab='sigma')
  324. term.plot(m3,parameter = 'sigma')
  325. par(mfrow=c(1,1))
  326. wp(m3)
  327.  
  328.  
  329. #media nao linear e sigma nao linear
  330. m4<-gamlss(y~pb(x1),sigma.fo=~pb(x1))
  331. edfAll(m4) #gl>2 indica nao linear
  332. par(mfrow=c(2,2))
  333. plot(x1,6*sin(x1*pi/4),ylab='mu')
  334. term.plot(m4,parameter = 'mu')
  335. plot(x1,5*sin(x1*pi/6)+6,ylab='sigma')
  336. term.plot(m4,parameter = 'sigma')
  337. par(mfrow=c(1,1))
  338. wp(m4,ylim.all = 2)
  339.  
  340. AIC(m2,m3,m4)
  341. BIC(m2,m3,m4)
  342.  
  343. #Valores esperados
  344. plot(x1,y)
  345. lines(x1[order(x1)],m4$mu.fv[order(x1)],col=2,lwd=3)
  346.  
  347.  
  348. dados=data.frame(y,x1)
  349.  
  350. #media nao linear e sigma constante
  351. plotSimpleGamlss(y,x1, model=m2, data=dados,
  352. x.val=seq(1,10,1),val=4, N=1000)
  353.  
  354. #media nao linear e sigma linear
  355. plotSimpleGamlss(y,x1, model=m3, data=dados,
  356. x.val=seq(1,10,1),val=4, N=1000)
  357.  
  358. #media nao linear e sigma nao linear
  359. plotSimpleGamlss(y,x1, model=m4, data=dados,
  360. x.val=seq(1,10,1),val=4, N=1000)
  361.  
  362. #percentis
  363.  
  364. #media nao linear e sigma constante
  365. centiles(m2, xvar=x1, lwd=2,cent=c(5,50,95))
  366. centiles(m3, xvar=x1, lwd=2,cent=c(5,50,95))
  367. centiles(m4, xvar=x1, lwd=2,cent=c(5,50,95))
  368.  
  369.  
  370.  
  371.  
  372. #### Exemplo 7####
  373. library(gamlss.dist)
  374.  
  375. set.seed(10)
  376. a<-rPE2(200,5,1,2)
  377. #mesocurtica
  378. curve(dPE2(x,5,1,2),2,8,ylim=c(0,0.8),lwd=4,xlab='Y',ylab="Densidade")
  379.  
  380. set.seed(10)
  381. b<-rPE2(200,5,1,1)
  382. #leptocurtica
  383. curve(dPE2(x,5,1,1),0,10,col='chartreuse1',lwd=4,add=T)
  384.  
  385. set.seed(10)
  386. c<-rPE2(200,5,1,5)
  387. #platicurtica
  388. curve(dPE2(x,5,1,5),0,10,col='deepskyblue',lwd=4,add=T)
  389.  
  390. y=c(a,b,c)
  391. tipo<-rep(c('a','b','c'),each=200)
  392.  
  393. dados<-data.frame(y,tipo)
  394. head(dados)
  395.  
  396.  
  397. #Normal
  398. m1<-gamlss(y~tipo,sigma.fo=~tipo,data=dados)
  399. #PE2
  400. m2<-gamlss(y~tipo,sigma.fo=~tipo,nu.fo=~tipo,
  401. family = PE2,n.cyc=1500)
  402.  
  403.  
  404. summary(m1)
  405. summary(m2)
  406.  
  407. #Normal
  408. m1<-gamlss(y~1,sigma.fo=~tipo,data=dados)
  409. #PE2
  410. m2<-gamlss(y~1,sigma.fo=~1,nu.fo=~tipo,family = PE2)
  411.  
  412.  
  413.  
  414. hist(a,probability = T,main = 'mesocurtica',ylim=c(0,0.8))
  415. curve(dnorm(x,m1$mu.fv[1],m1$sigma.fv[1]),add=T,lwd=3,lty=1)
  416. curve(dPE2(x,m2$mu.fv[1],m2$sigma.fv[1],m2$nu.fv[1]),add=T,lwd=4,lty=2,col='2')
  417.  
  418. hist(b,probability = T,main='leptocúrtica',ylim=c(0,0.8),breaks = 30)
  419. curve(dnorm(x,m1$mu.fv[400],m1$sigma.fv[400]),add=T,lwd=3)
  420. curve(dPE2(x,m2$mu.fv[400],m2$sigma.fv[400],m2$nu.fv[400]),add=T,lwd=3,lty=2,col=2)
  421.  
  422. hist(c,probability = T,main='platicúrtica',ylim=c(0,0.90))
  423. curve(dnorm(x,m1$mu.fv[600],m1$sigma.fv[600]),add=T,lwd=3)
  424. curve(dPE2(x,m2$mu.fv[600],m2$sigma.fv[600],m2$nu.fv[600]),col=2,add=T,lwd=4,lty=2)
  425.  
  426.  
  427. AIC(m1,m2)
  428. BIC(m1,m2)
Add Comment
Please, Sign In to add comment