-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgls_audiogram_bs.R
54 lines (40 loc) · 2.59 KB
/
pgls_audiogram_bs.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
#pgls_todo_hm<-pgls_todo_nogeomet[seq(2,length(pgls_todo_nogeomet),2)]
#Head mass only
#modellist<-pgls_todo_hm
pgls_models_list_bs<-lapply(modellist_bs,pgls_models)#run pgls
#make list of dataframes with the PGLS outputs.
tbllist_audiogram<-list()
for (i in seq_along(pgls_models_list_bs)){#change th 'Model' colume in this as appropriate
tbllist_audiogram[[i]]<-as.data.frame(summary(pgls_models_list_bs[[i]])$'coefficients')
tbllist_audiogram[[i]]$Adj_Rsquared<-summary(pgls_models_list_bs[[i]])$'adj.r.squared'[1]#rsquared
tbllist_audiogram[[i]]$Model<-rep(modellist_bs[i],2)#formula<_____________________CHECK FORMULA LIST HERE is correct
tbllist_audiogram[[i]]$Lambda<-summary(pgls_models_list_bs[[i]])$'param'[[2]]#lambda
tbllist_audiogram[[i]]$Fstat<-summary(pgls_models_list_bs[[i]])$fstatistic[1]
tbllist_audiogram[[i]]$Fstat_numdf<-summary(pgls_models_list_bs[[i]])$fstatistic[2]
tbllist_audiogram[[i]]$Fstat_dendf<-summary(pgls_models_list_bs[[i]])$fstatistic[3]
}
#organize the dataframe table (significant digist, remove redundant F stat & R squared)
for(i in seq_along(tbllist_audiogram)){
tbllist_audiogram[[i]]$Coefficients<-row.names(tbllist_audiogram[[i]])
tbllist_audiogram[[i]]$Coefficients<-gsub('[[:digit:]]+', '', tbllist_audiogram[[i]]$Coefficients)#regex to remove number automatically added during the loop
#identify numeric cols and character cols to apply the significant digits function
character_cols<-unlist(lapply(tbllist_audiogram[[i]], is.character))
numeric_cols <- unlist(lapply(tbllist_audiogram[[i]], is.numeric))# Identify numeric columns
tbllist_audiogram[[i]]<-cbind(tbllist_audiogram[[i]][,which(character_cols)],signif(tbllist_audiogram[[i]][,which(numeric_cols)], digits = 3))
colnames(tbllist_audiogram[[i]])[6]<-"P.val"#rename b/c flextable doesn't work will with the '>' sign
row.names(tbllist_audiogram[[i]])<-c()#remove row names
print(tbllist_audiogram[[i]])
}
audiogrampgls_bs<-do.call(rbind.data.frame,tbllist_audiogram)
audiogrampgls_bs$category<-rep(categorylist_bs,each = 2)
audiogrampgls_bs <- subset(audiogrampgls_bs, select = c(category,Model,Coefficients,Estimate, `Std. Error`,P.val,Adj_Rsquared,Lambda))
flexall<-flextable(audiogrampgls_bs) %>% add_header_lines(
values = "Table X. Models for selection") %>%
bold(i = ~ P.val < 0.05) %>% # select columns add: j = ~ Coefficients + P.val
autofit()
flexall
#pgls diagnostics
#write table to word file
toprint<-read_docx() #create word doc object
body_add_flextable(toprint,flexall)#add pgls output table
body_end_section_landscape(toprint)