PeiYau_Lung

Untitled

Sep 30th, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.92 KB | None | 0 0
  1. #-------------------------------
  2. #     SINGLE VERSION
  3. #-------------------------------
  4. N=dim(labor1)[1]
  5. set.seed(1)
  6.  
  7. ## reduced.prop is a matrix that stores the proportion of targeting the original model when we reduce the sample size. The first row stores the proportions of BIC and the second row stores the proportions of AIC
  8. reduced.prop=matrix(NA, 2, 28)
  9. judge.BIC=rep(FALSE,100); judge.AIC=rep(FALSE,100)
  10. for (T in seq(285, 420, 5))#
  11. {
  12.   for (i in 1:100)
  13.   {
  14.     ind=sample(N, T)
  15.     train=labor1[ind,]
  16.     reg.all=lm(lnearnings ~ WA+I(WA^2)+WE+kids+HA+HE+WMED+WFED+UN+CIT+AX+MTR+lnearning.H, data=train)
  17.    
  18.     train.BIC=step(reg.all,scope=list(upper=~.,lower=~1),k=log(T),trace=0)
  19.     judge.BIC[i]=(train.BIC$terms[[3]]==m.BIC$terms[[3]])
  20.    
  21.     train.AIC=step(reg.all,scope=list(upper=~.,lower=~1),trace=0)
  22.     judge.AIC[i]=(train.AIC$terms[[3]]==m.AIC$terms[[3]])
  23.   }
  24.   reduced.prop[1,T/5 - 56]=sum(judge.BIC)/100
  25.   reduced.prop[2,T/5 - 56]=sum(judge.AIC)/100
  26. }
  27. reduced.prop
  28.  
  29. #---------------------------------------------
  30. #     PARALLEL COMPUTING
  31. #---------------------------------------------
  32. library(snowfall)  
  33. sfInit(parallel = TRUE, cpus = 7, type="SOCK")
  34. sfExport('reg.all','N','labor1','m.BIC','m.AIC')
  35.  
  36. inner.loop = function(T){
  37.   reg.all = apply(matrix(1:100,100,1),1,function(i){
  38.     ind = sample(N,T)
  39.     train=labor1[ind,]
  40.     reg.all=lm(lnearnings ~ WA+I(WA^2)+WE+kids+HA+HE+WMED+WFED+UN+CIT+AX+MTR+lnearning.H, data=train)
  41.    
  42.     train.BIC=step(reg.all,scope=list(upper=~.,lower=~1),k=log(T),trace=0)
  43.     judge.BIC=(train.BIC$terms[[3]]==m.BIC$terms[[3]])
  44.    
  45.     train.AIC=step(reg.all,scope=list(upper=~.,lower=~1),trace=0)
  46.     judge.AIC=(train.AIC$terms[[3]]==m.AIC$terms[[3]])
  47.     return(list(judge.BIC,judge.AIC))
  48.    
  49.   })
  50.   reg.all = matrix(unlist(reg.all),2,100)
  51.   return(list(mean(reg.all[1,]),mean(reg.all[2,])))
  52. }
  53.  
  54. reduced.prop <- sfSapply(seq(285,420,5), inner.loop)
Advertisement
Add Comment
Please, Sign In to add comment