---
title: "Faktorska analiza"
output: 
  flexdashboard::flex_dashboard:
    social: menu
    orientation: columns
    vertical_layout: fill
    source_code: embed
---

```{css, echo=FALSE}
.sidebar { overflow: auto; }
.dataTables_scrollBody {
    height:95% !important;
    max-height:95% !important;
}
.chart-stage-flex {
    overflow:auto !important;
}
```

```{r setup, include=FALSE}
library(DT)
library(knitr)
library(kableExtra)
library(psych)
library(readxl)
library(tidyverse)
library(corrplot)
library(svgPanZoom)
library(lavaan)
library(parameters)

cols <- c("osoba", "DYNAMICS", "AVERAGE_COMPARISON", "REQUESTS", "MESSAGES_SU",
          "EXTRACURRICULAR", "STATUS_COMPARISON", "PREDICTION_GROUP", 
          "TEACHERS", "BADGES", "ABSENCE", "TIMELINE", "AVERAGE", "ASSESSMENT_EXAMS", "ASSESSMENT_CONT")
podaci_EFA <- read_excel('potrebe_studenata_2017.xlsx', sheet=1) %>% select(-one_of(cols))
new_cols <- c("osoba",  "DASHBOARD", "DASHBOARD_CUSTOM",  "ABSENCE", "TIMELINE", "AVERAGE", "ASSESSMENT_EXAMS", "ASSESSMENT_CONT")
podaci_CFA <- read_excel('potrebe_studenata_2022.xlsx', sheet=1) %>% select(-one_of(new_cols))
EFA_model <- fa(podaci_EFA, nfactors = 5)
file.name <- './graf3.svg'
in.svg <- readChar(file.name, nchars = file.info(file.name)$size)

model1 <- efa_to_cfa(EFA_model, threshold = 0.6)
#model1 <- efa_to_cfa(EFA_model)
model2 <- 'MR2 =~ COMPARISON_EXAMS + COMPARISON_ECTS + COMPARISON_GRADES + COMPARISON_OBLIGATIONS + STATUS_STUDENTS + ECTSAVG_STUDY + ECTSAVG_COURSE + AVGGRADE_COURSE + PASSRATE_COURSE + COMPARISON_OTHERS
MR1 =~ SCHEDULE + CALENDAR + COURSES + NOTIFICATIONS_COURSE + NOTIFICATIONS_DEADLINES + MESSAGES_TEACHER + MESSAGES_ADMIN + PPLAN_COURSE + PPLAN_STUDY + PPLAN_MONITORING + PPLAN_EXTRACURRICULAR + NOTIFICATIONS_PRECLASS + SUGGESTION_TEAMS + ESTIMATION_TIME + SUGGESTION_LEARNING + ECTS_MONITORING
MR3 =~ RISK + PREDICTION_COURSE + PREDICTION_YEAR + PREDICTION_OBLIGATIONS + PREDICTION_ECTS + PREDICTION_PROGRAM + STATUS_CURRENT
MR4 =~ BADGES_COMPARISON + BADGES_COLLECTING + AWARDS + COMPETITIONS
MR5 =~ TEACHER_EVALUATION + TEACHER_PROFILE + TEACHERS_CONTEVAL + TEACHERS_FEEDBACK'

rez1 <- cfa(model1, podaci_CFA)
model2 <- efa_to_cfa(EFA_model)
rez2 <- cfa(model2, podaci_CFA)
```

Deskriptiva {data-navmenu="Podaci"}
=======================================================================

Column {.tabset .tabset-fade}
-----------------------------------------------------------------------

### Godina 2017.

```{r}
datatable(round(describe(podaci_EFA),2), filter = 'top',
    extensions = c('FixedColumns','Select', 'Buttons'),
    options = list(
      dom = 'Blfrtip',
      select = list(style = 'os', items = 'row'),
      buttons = c('selectAll', 'selectNone', 'selectRows', 'selectColumns', 'selectCells'),
      pageLength = 15,
      scrollX = TRUE,
      scrollY= TRUE,
      fixedColumns = TRUE
    )
)
```

### Godina 2022.

```{r}
datatable(round(describe(podaci_CFA),2), filter = 'top',
    extensions = c('FixedColumns','Select', 'Buttons'),
    options = list(
      dom = 'Blfrtip',
      select = list(style = 'os', items = 'row'),
      buttons = c('selectAll', 'selectNone', 'selectRows', 'selectColumns', 'selectCells'),
      pageLength = 15,
      scrollX = TRUE,
      scrollY= TRUE,
      fixedColumns = TRUE
    )
)
```

Deskriptiva - slike {data-navmenu="Podaci"}
=======================================================================

Column {data-width=400}
-----------------------------------------------------------------------

### Godina 2017.

```{r,fig.width = 12,fig.height=11}
error.dots(podaci_EFA, tail=30, head=30, sort=FALSE)
```

Column {data-width=400}
-----------------------------------------------------------------------

### Godina 2022.

```{r,fig.width = 12,fig.height=11}
error.dots(podaci_CFA, tail=30, head=30, sort=FALSE)
```

Korelacije {data-navmenu="Podaci"}
=======================================================================

Column {.tabset .tabset-fade}
-----------------------------------------------------------------------

### Godina 2017.

```{r}
datatable(round(corr.test(podaci_EFA, use = "pairwise.complete.obs", minlength=50)$ci, 2), filter = 'top',
    extensions = c('FixedColumns','Select', 'Buttons'),
    options = list(
      dom = 'Blfrtip',
      select = list(style = 'os', items = 'row'),
      buttons = c('selectAll', 'selectNone', 'selectRows', 'selectColumns', 'selectCells'),
      pageLength = 15,
      scrollX = TRUE,
      fixedColumns = TRUE
    )
)
```

### Godina 2022.

```{r}
datatable(round(corr.test(podaci_CFA, use = "pairwise.complete.obs", minlength=50)$ci, 2), filter = 'top',
    extensions = c('FixedColumns','Select', 'Buttons'),
    options = list(
      dom = 'Blfrtip',
      select = list(style = 'os', items = 'row'),
      buttons = c('selectAll', 'selectNone', 'selectRows', 'selectColumns', 'selectCells'),
      pageLength = 15,
      scrollX = TRUE,
      fixedColumns = TRUE
    )
)
```


Korelacije - slike {data-navmenu="Podaci"}
=======================================================================

## Column1 {.tabset .tabset-fade}

### korelacije 2017.

```{r}
corrplot(round(cor(podaci_EFA, use = "pairwise.complete.obs"), 2), type = "upper", tl.col = "black", tl.srt = 90,
         diag=FALSE, mar=c(0,0,0.1,0), tl.cex=0.4)
```

### p-vrijednosti 2017.

```{r}
corrplot(round(corr.test(podaci_EFA, use = "pairwise.complete.obs")$p, 2), type = "upper", tl.col = "black", tl.srt = 90,
         diag=FALSE, mar=c(0,0,0.1,0), tl.cex=0.4)
```

### korelacije 2022.

```{r}
corrplot(round(cor(podaci_CFA, use = "pairwise.complete.obs"), 2), type = "upper", tl.col = "black", tl.srt = 90,
         diag=FALSE, mar=c(0,0,0.1,0), tl.cex=0.4)
```

### p-vrijednosti 2022.

```{r}
corrplot(round(corr.test(podaci_CFA, use = "pairwise.complete.obs")$p, 2), type = "upper", tl.col = "black", tl.srt = 90,
         diag=FALSE, mar=c(0,0,0.1,0), tl.cex=0.4)
```

## Column1 {.tabset .tabset-fade}

### korelacije 2022.

```{r}
corrplot(round(cor(podaci_CFA, use = "pairwise.complete.obs"), 2), type = "upper", tl.col = "black", tl.srt = 90,
         diag=FALSE, mar=c(0,0,0.1,0), tl.cex=0.4)
```

### p-vrijednosti 2022.

```{r}
corrplot(round(corr.test(podaci_CFA, use = "pairwise.complete.obs")$p, 2), type = "upper", tl.col = "black", tl.srt = 90,
         diag=FALSE, mar=c(0,0,0.1,0), tl.cex=0.4)
```

### korelacije 2017.

```{r}
corrplot(round(cor(podaci_EFA, use = "pairwise.complete.obs"), 2), type = "upper", tl.col = "black", tl.srt = 90,
         diag=FALSE, mar=c(0,0,0.1,0), tl.cex=0.4)
```

### p-vrijednosti 2017.

```{r}
corrplot(round(corr.test(podaci_EFA, use = "pairwise.complete.obs")$p, 2), type = "upper", tl.col = "black", tl.srt = 90,
         diag=FALSE, mar=c(0,0,0.1,0), tl.cex=0.4)
```

Unutarnja pouzdanost {data-navmenu="EFA 2017"}
=======================================================================

Column {data-width=400}
-----------------------------------------------------------------------

### Cronbach alpha

```{r, attr.output='style="max-height: 100%;"'}
psych::alpha(podaci_EFA)
```

Column {data-width=400}
-----------------------------------------------------------------------

### split-half


```{r, attr.output='style="max-height: 100%;"'}
splitHalf(podaci_EFA)
```

### Kaiser-Meyer-Olkin

```{r, attr.output='style="max-height: 100%;"'}
KMO(podaci_EFA)
```

EFA - broj faktora {data-navmenu="EFA 2017"}
=======================================================================

Column {data-width=300}
-----------------------------------------------------------------------

### Svojstvene vrijednosti matrice korelacija

```{r warning=FALSE}
EFA_cor <- cor(podaci_EFA, use = "pairwise.complete.obs")
sv <- eigen(EFA_cor)
sv$values
```

Column {data-width=500}
-----------------------------------------------------------------------

### SCREE plot

```{r warning=FALSE,fig.width=8, fig.height=7}
scree(podaci_EFA)
```

EFA model {data-navmenu="EFA 2017"}
=======================================================================

Column {data-width=400}
-----------------------------------------------------------------------

### EFA - 5 faktora

```{r, attr.output='style="max-height: 100%;"'}
EFA_model
```

Column {data-width=400}
-----------------------------------------------------------------------

### Loadings (cutoff = 0.4)

```{r, attr.output='style="max-height: 100%;"'}
print(EFA_model$loadings, sort=TRUE, cutoff=0.6)
```

Loadings {data-navmenu="EFA 2017"}
=======================================================================

Column {.tabset .tabset-fade}
-----------------------------------------------------------------------

### Interaktivna tablica

```{r}
datatable(round(unclass(EFA_model$loadings),2), filter = 'top',
    extensions = c('FixedColumns','Select', 'Buttons'),
    options = list(
      dom = 'Blfrtip',
      select = list(style = 'os', items = 'row'),
      buttons = c('selectAll', 'selectNone', 'selectRows', 'selectColumns', 'selectCells'),
      pageLength = 15,
      scrollX = TRUE,
      fixedColumns = TRUE
    )
)
```

### graf

```{r}
svgPanZoom(in.svg, controlIconsEnabled = TRUE)
```

Scores {data-navmenu="EFA 2017"}
=======================================================================

Column {data-width=400}
-----------------------------------------------------------------------

### Funkcija gustoće

```{r warning=FALSE}
plot(density(EFA_model$scores, na.rm = TRUE), 
    main = "Factor Scores")
```

Column {data-width=400}
-----------------------------------------------------------------------

### tablica

```{r,attr.output='style="max-height: 100%;"'}
EFA_model$scores
```

Summary {data-navmenu="CFA 2022"}
=======================================================================

Column {.tabset .tabset-fade}
-----------------------------------------------------------------------

### Model (cutoff=0.4)

```{r}
model1
```

### Summary (cutoff=0.4)

```{r}
summary(rez1, fit.measures=TRUE)
```

### Sve mjere testa (cutoff=0.4)

```{r}
fitMeasures(rez1)
```

Column {.tabset .tabset-fade}
-----------------------------------------------------------------------

### Model (sve stare varijable)

```{r}
model2
```

### Summary (sve stare varijable)

```{r}
summary(rez2, fit.measures=TRUE)
```

### Sve mjere testa (sve stare varijable)

```{r}
fitMeasures(rez2)
```

Parametri {data-navmenu="CFA 2022"}
=======================================================================

Column {.tabset .tabset-fade}
-----------------------------------------------------------------------

### Parametri (cutoff=0.4)

```{r}
parameterEstimates(rez1) %>%
  kbl(caption = "Parametri (cutoff=0.4)") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

### Parametri (sve stare varijable)

```{r}
parameterEstimates(rez2) %>%
  kbl(caption = "Parametri (sve stare varijable)") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

Normalizirani parametri {data-navmenu="CFA 2022"}
=======================================================================

Column {.tabset .tabset-fade}
-----------------------------------------------------------------------

### Normalizirani parametri (cutoff=0.4)

```{r}
standardizedSolution(rez1) %>%
  kbl(caption = "Normalizirani parametri (cutoff=0.4)") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

### Normalizirani parametri (sve stare varijable)

```{r}
standardizedSolution(rez2) %>%
  kbl(caption = "Normalizirani parametri (sve stare varijable)") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```
