17 /01/2018


```{r}
library(readr)
library(dplyr)
library(metafor)
library(devtools)
library(patchwork)
library(purrr)
library(tidyverse)
library(tibble)
```


##  test for meta-meta-analysis / collapse confounded parameters!



```{r}
meta1 <- read.csv("meta.csv") # cleaned up data/ cleaned up parameter groups. 
unique(meta1$procedure) #19. Can still get reduced.
unique(meta1$GroupingTerm) #10. sounds reasonable
unique(meta1$parameter_group) # 152 levels. To be used as random factor in meta-meta!
```

```{r meta-meta}
# use meta1 , newly loaded (as has been cleaned / checked in excel)

# need to write function to collapse the correlated parametrs down (based on parameter_groups).  For the parameter_groups that are represented more than 1, run meta analysis, the other traits stay as are.  rma(yi = estimate i.e. ln RR / ln CVR / ln VR, se = lnRR_se / lnCVR_se / lnVR_se, method = "FE" )


meta1[,"MetaMeanV"] <-meta1$lnRR_se^2
meta1[,"MetaCVRV"] <-meta1$lnCVR_se^2
meta1[,"MetaVRV"] <-meta1$lnVR_se^2
```


workflow:
count parameter_names per parameter_group, save in new list "par_group_size":
```{r}
meta1 %>%  count(parameter_group)
```
dataframe: most groups have only 1 , it;s only a couple where there's multiple parameter names.


```{r}
meta1b <-
  meta1 %>%
  group_by(parameter_group) %>% 
  summarize(par_group_size = length(unique(parameter_name, na.rm = TRUE)))
#this gives me a summary of number of parameter names in each parameter group, now I neeed to merge it back together


meta1$par_group_size <- meta1b$par_group_size[match(meta1$parameter_group, meta1b$parameter_group)]

```

create subsets: needs to use those that have more than 1 count (par_group_size)


```{r}
meta1_sub<-subset(meta1,par_group_size >1) # 86 observations
```

NEST
```{r}
n_count <- meta1_sub %>% 
  group_by(parameter_group) %>% 
  nest()
```

FIXED EFFECTS MODEL

```{r}
sub_model <- n_count %>%
  mutate(model= map(data, ~ rma(yi = lnRR, sei = lnRR_se, method = "FE", data= .)))
```

 unknot this somehow
 
 THIS IS NOT WORKING
```{r}

b_fun <- function(mod_sub)
  coefficients(mod_sub[[1]])

sub_model %>% transmute(parameter_group, estimate = map(model, b_fun))


```