-
Notifications
You must be signed in to change notification settings - Fork 0
/
bikeshare.R
79 lines (63 loc) · 2.52 KB
/
bikeshare.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
library(glmnet)
library(dummies)
library(grplasso)
setwd(getwd())
source("functions_refit_bic.R")
mydata=read.csv("bikehour.csv",header=T)
y=log(mydata[,ncol(mydata)])
mydata$weathersit=(mydata$weathersit==1)
cate_x=mydata[,c(3:10)]
cate_x=as.data.frame(sapply(cate_x,as.character))
temp=rep(0,ncol(cate_x))
for(i in 1:ncol(cate_x)){
temp[i]=length(levels(cate_x[,i]))
}
index=c(NA,rep(c(1:length(temp)),(temp-1)))
temp0=rep(0,length(temp))
for(i in 1:length(temp)){
temp0[i]=sum(temp[1:i])
}
cate_x=dummy.data.frame(cate_x)
cate_x=cate_x[,-temp0]
scale_x=scale(mydata[,11:16])
x=cbind(1,scale(cate_x),scale_x)
data=as.matrix(data.frame(x,y))
index=c(index,(max(index,na.rm=T)+1):(max(index,na.rm=T)+6))
dim(data)
head(data)
p=ncol(data)-2 ###covariate number
n=nrow(data)
tau=0.8
iter.num=50
outputPath=paste("./results_bike")
if(!dir.exists(outputPath)){
dir.create(outputPath)
}
##################################################################################
#################################################
################ gamma=0.6 ######################
#grouplasso
res_06<-BLB_grp(data,0.6,index,iter.num,tau,s=30,
family="gaussian")
write.csv(res_06$beta_hat,paste(outputPath,"/beta_06.csv",sep=""),row.names = F)
write.csv(res_06$tim_cum,paste(outputPath,"/time_06.csv",sep=""),row.names = F)
################ gamma=0.7 ######################
res_07<-BLB_grp(data,0.7,index,iter.num,tau,s=20,
family="gaussian")
write.csv(res_07$beta_hat,paste(outputPath,"/beta_07.csv",sep=""),row.names = F)
write.csv(res_07$tim_cum,paste(outputPath,"/time_07.csv",sep=""),row.names = F)
################ gamma=0.8 ######################
res_08<-BLB_grp(data,0.8,index,iter.num,tau,s=10,
family="gaussian")
write.csv(res_08$beta_hat,paste(outputPath,"/beta_08.csv",sep=""),row.names=F)
write.csv(res_08$tim_cum,paste(outputPath,"/time_08.csv",sep=""),row.names=F)
################ gamma=0.9 ######################
res_09<-BLB_grp(data,0.9,index,iter.num,tau,s=10,
family="gaussian")
write.csv(res_09$beta_hat,paste(outputPath,"/beta_09.csv",sep=""),row.names=F)
write.csv(res_09$tim_cum,paste(outputPath,"/time_09.csv",sep=""),row.names=F)
######### bootstrap
res_boot<-BOOT_grp(data,index,iter.num=500,tau,
family="gaussian")
write.csv(res_boot$beta_hat,paste(outputPath,"/beta_boot.csv",sep=""),row.names=F)
write.csv(res_boot$tim_cum,paste(outputPath,"/time_boot.csv",sep=""),row.names=F)