-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarianceEstimation.R
102 lines (90 loc) · 3.14 KB
/
varianceEstimation.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Variance estimation according Chia et al
VarianceEstCore <- function(parameterTable, formStr){
vcompNames=c(rev(gsub("1 \\| ","",attr(terms(as.formula(paste0("1",formstr))),"term.labels"))),"Residual")
data = select(parameterTable,Frame0:Frame100)
X = parameterTable
Ynames = names(select(data,Frame0:Frame100))
V=lapply(1:ncol(data),function(j){
form=as.formula(paste0(Ynames[j],formstr))
lmm=lmer(form,data=X,control=lmerControl())
out=as.data.frame(VarCorr(lmm))[,c("grp","sdcor")]
names(out)[2]=Ynames[j]
out$grp=factor(vcompNames,levels=vcompNames[c((length(vcompNames)-1):1,length(vcompNames))])
out
})
pV=Reduce(merge,V)
pV=tidyr::gather(pV,Frames,sd,-grp,convert=T)
pV$sd=round(pV$sd,5)
sdData = tidyr::spread(pV,Frames,sd)
return(list(gather = pV,spread = sdData))
}
plotVariance<-function(varianceTable){
ggplot(data = varianceTable$gather,aes(x=Frames, y=sd,group = grp,color=grp ))+geom_line()+
theme(panel.grid.minor = element_blank(),
axis.title.x = element_text(size = 10),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(size = 10),
axis.text.y = element_text(size=8),
legend.text = element_text(size = 8),
legend.title=element_blank(),
plot.title = element_text(family="Times", face="plain", size=10),
legend.position='top')
}
getGlobalVariance<-function(varianceTable){
outTable = varianceTable$gather %>%
group_by(grp)%>%
summarize(globalSD = round(sqrt(mean(sd^2)),1))
return(outTable)
}
# TODO - Resume this code----------
# core model fucntion
# VarianceEstCore <- function(.data, formStr){
# form=as.formula(paste0("Value~",formStr))
# lmm=lmer(form,data=.data,control=lmerControl())
# out=as.data.frame(VarCorr(lmm))[,c("grp","sdcor")]
#
# return(out)
#
# }
#
#
# main variance estimation
# varianceEstimation <-function(Data,Label,Axis,Context,formStr){
# if (Context == "Overall"){
# angles = filter(Data, Label==Label & Axis == Axis )
#
# VarianceAtEachFrames = angles %>%
# gather(Frames,Value,Frame0:Frame100)%>%
# group_by(Frames) %>%
# do(data.frame(vari=VarianceEstCore(.,formStr)))
#
# VarianceAtEachFrames["Label"]=Label
# VarianceAtEachFrames["Axis"]=Axis
#
# VarianceGlobal = VarianceAtEachFrames %>%
# group_by(Label,Axis,vari.grp) %>%
# summarize( sd = round(sqrt(mean(vari.sdcor^2)),1))
#
#
# } else {
# angles = filter(Data, Label==Label & Axis == Axis & Context==Context)
#
# VarianceAtEachFrames = angles %>%
# gather(Frames,Value,Frame0:Frame100)%>%
# group_by(Frames) %>%
# do(data.frame(vari=VarianceEstCore(.,formStr)))
#
# VarianceAtEachFrames["Label"]=Label
# VarianceAtEachFrames["Axis"]=Axis
# VarianceAtEachFrames["Context"]=Context
#
#
# VarianceGlobal = VarianceAtEachFrames %>%
# group_by(Label,Axis,Context,vari.grp) %>%
# summarize( sd = round(sqrt(mean(vari.sdcor^2)),1))
# }
#
#
# return (list(frames=VarianceAtEachFrames,overall =VarianceGlobal))
# }