---
title: "Faktorska analiza"
output: 
  html_document:
    toc: true
    toc_float:
       collapsed: true
       smooth_scroll: true
    toc_depth: 3
    number_sections: true
---

```{css, echo=FALSE}
div.main-container {
  max-width: 1600px !important;
}

.header-section-number::after {
  content: ".";
}
```

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r message=FALSE}
library(DT)
library(psych)
library(readxl)
library(tidyverse)
library(corrplot)
```

```{r warning=FALSE}
podaci <- read_excel('potrebe_studenata_2017.xlsx', sheet=1) %>% select(!osoba)
```

```{r warning=FALSE}
head(podaci)
```

# Deskriptivna statistika čitavog skupa podataka

## Tablica

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

## error.dots

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


## error.bars

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

# Korelacije

## Matrica korelacija

```{r warning=FALSE}
datatable(round(cor(podaci, use = "pairwise.complete.obs"), 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
    )
)
```

## p-vrijednosti

```{r warning=FALSE}
datatable(round(corr.test(podaci, use = "pairwise.complete.obs")$p, 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
    )
)
```

## Intervali pouzdanosti

```{r warning=FALSE}
datatable(round(corr.test(podaci, use = "pairwise.complete.obs")$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
    )
)
```

# Unutarnja pouzdanost

## Cronbach alpha

```{r warning=FALSE}
psych::alpha(podaci)
```

## split-half

```{r warning=FALSE}
splitHalf(podaci)
```

# EFA

## Broj faktora

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

```{r warning=FALSE,fig.width=9, fig.height=8}
scree(EFA_cor, factors = FALSE)
```

## EFA model s 10 faktora

```{r warning=FALSE}
EFA_model <- fa(podaci, nfactors = 10)
```

```{r warning=FALSE}
EFA_model
```

## Loadings

```{r warning=FALSE}
print(EFA_model$loadings, sort=TRUE, cutoff=0.4)
```

## Scores

```{r warning=FALSE}
head(EFA_model$scores)
```

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

```{r,fig.width=9,fig.height=9}
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)
```


```{r,fig.width=9,fig.height=9}
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)
```