---
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(psych)
library(readxl)
library(tidyverse)
library(corrplot)
library(svgPanZoom)

podaci <- read_excel('potrebe_studenata_2017.xlsx', sheet=1) %>% select(!osoba)
EFA_model <- fa(podaci, nfactors = 5)
file.name <- './graf2.svg'
in.svg <- readChar(file.name, nchars = file.info(file.name)$size)
```

Deskriptiva
=======================================================================

```{r}
datatable(round(describe(podaci),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
=======================================================================

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

### error.dots

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

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

### error.bars

```{r,fig.width = 12,fig.height=11}
error.bars(podaci)
```

Korelacije
=======================================================================

```{r}
datatable(round(corr.test(podaci, 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
=======================================================================

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

### koeficijenti korelacija

```{r}
corrplot(round(cor(podaci, 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)
```

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

### p-vrijednosti korelacija

```{r}
corrplot(round(corr.test(podaci, 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
=======================================================================

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

### Cronbach alpha

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

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

### split-half


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

### Kaiser-Meyer-Olkin

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

EFA - broj faktora
=======================================================================

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

### Svojstvene vrijednosti matrice korelacija

```{r warning=FALSE}
EFA_cor <- cor(podaci, 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 model
=======================================================================

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.4)
```

Loadings
=======================================================================

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
=======================================================================

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
```
