-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFA4Met.Rmd
147 lines (117 loc) · 3.45 KB
/
FA4Met.Rmd
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
title: "Factor analysis"
author: "Ye Bi"
date: "`r Sys.Date()`"
output: html_document
---
```{r}
#test commit and push
library(blavaan)
# install.packages("pheatmap")
library(pheatmap)
# install.packages("psych")
library(psych)
# install.packages("dplyr")
library(dplyr)
# install.packages("tidyr")
library(tidyr)
# install.packages("knitr")
library(knitr)
```
```{r}
input_load = function(treatment){
met_rr = read.csv(paste0("../../Met/met_rr_", treatment, ".csv"))
return(met_rr)
}
met_rr_Control = input_load("Control")
met_rr_Stress = input_load("Stress")
```
```{r}
source("../../functions.R")
met_names = met_name_func(mode = 'name')
```
```{r}
heatmap4FA_func <- function(input, treatment, met_names,fff, threshold, log, FAnames, maintitle){
y = input[,-c(1:2)]
colnames(y) = met_names
# phenotypic correlation among traits
COR <- cor(y, use = "complete.obs")
# fit a maximum likelihood-based EFA model by assuming fff factors
fit.mle <- fa(COR, nfactors = fff, fm = "ml", max.iter = 1000,
rotate = "varimax")
# extract factor loading coefficients
loading <- round(fit.mle$loadings, digits = 10)
colnames(loading) = FAnames
loading_new = loading[1:66, 1:ncol(loading)]
maxposition = apply(loading_new, 1, function(x){which.max(abs(x))})
loading_prep = matrix('', 66, ncol(loading))
colnames(loading_prep) = colnames(loading_new)
rownames(loading_prep) = rownames(loading_new)
for(i in 1:66){
pp = maxposition[i]
if(abs(loading_new[i,pp]) >= threshold){
loading_prep[i, pp] = round(loading_new[i, pp], 2)}
}
nn = loading_prep
nn0 = round(loading, 2)
pp = pheatmap(loading,
display_numbers = nn,
cluster_cols = FALSE,
cluster_rows = T,
cutree_cols = fff,
angle_col = 0,
main = maintitle,
treeheight_row=0,
treeheight_col = 0,
fontsize_number = 10,
border_color = FALSE)
print(pp)
dev.print(pdf, file=paste0("../temp/", log, "heatmap4FA_", treatment, ".pdf"), height=10, width=10)
return(list(nn=nn, pp = pp[[4]]))
}
```
```{r}
FA_conL = heatmap4FA_func(input = met_rr_Control,
treatment = 'control',
met_names = met_names,
fff=5,
threshold = 0.4,
log="",
FAnames = paste0("F",1:5),
maintitle = "(A)")
```
```{r}
FA_trtL = heatmap4FA_func(input = met_rr_Stress,
treatment = 'stress',
fff= 5,
met_names = met_names,
threshold = 0.4,
log='',
FAnames = paste0("F",6:10),
maintitle = "(B)")
```
```{r}
library('grid')
library("gridExtra")
grid.arrange(grobs = list(FA_conL[[2]],
FA_trtL[[2]]),
layout_matrix = matrix(c(1,2),1,2))
dev.print(pdf, file=paste0("../temp/heatmap4FA_combined.pdf"), height=8, width=16)
```
```{r}
FA_split_func <- function(FA){
FF = matrix(NA, 66, ncol(FA))
# rownames(FF) = rownames(FA)
rownames(FF) = met_names
colnames(FF) = colnames(FA)
for (i in 1:ncol(FA)){
FF[,i] = nzchar(FA[,i])
}
FF = as.data.frame(FF)
return(FF)
}
FmetL_con = FA_split_func(FA=FA_conL[[1]])
FmetL_trt = FA_split_func(FA=FA_trtL[[1]])
saveRDS(FmetL_con, "../Met/FmetL_con.RDS")
saveRDS(FmetL_trt, "../Met/FmetL_trt.RDS")
```