---
title: "Matematika 2 2022/2023"
output: 
  flexdashboard::flex_dashboard:
    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(flexdashboard)
library(tidyverse)
library(readxl)
library(corrplot)
library(knitr)
library(kableExtra)
library(naniar)
options(pillar.sigfig = 4)

studenti_RED_minus1 <- c("bcovic22@student.foi.hr",
                         "lkronast22@student.foi.hr",
                         "khozjan22@student.foi.hr",
                         "mkelemen22@student.foi.hr",
                         "dkurochki22@student.foi.hr")

raz <- read_excel("MAT2_2022_2023_RAZ.xlsx") %>% 
  select(mail)

izv <- read_excel("MAT2_2022_2023_IZV.xlsx") %>% 
  select(mail)

podaci <- read_excel("MAT2_2022_2023.xlsx") %>%
  mutate(Status = case_when(mail %in% izv$mail ~ "IZV",
                            mail %in% raz$mail ~ "RAZ",
                            .default = "RED"),
         OCJENA = case_when(mail %in% studenti_RED_minus1 ~ -1,
                            is.na(OCJENA) | OCJENA < 20 ~ 0,
                            OCJENA < 50 ~ 1,
                            OCJENA < 61 ~ 2,
                            OCJENA < 76 ~ 3,
                            OCJENA < 91 ~ 4,
                            .default = 5)) %>%
  mutate(DZ = rowSums(across(DZ1:DZ0), na.rm = TRUE), 
         KVIZ = DZ_KVIZ - DZ,
         ESEJ = ifelse(ESEJ == 0, NA, ESEJ)) %>%
  relocate(Status, .after = Prezime) %>%
  relocate(c(DZ:KVIZ, ESEJ:ANKETA), .before = KOL1) %>%
  select(Ime:KOL, USMENI, OCJENA) %>%
  mutate(UKUPNO = case_when(Status == "RED" ~ rowSums(across(DZ:KOL3), na.rm = TRUE),
                            .default = rowSums(across(ESEJ:KOL3), na.rm = TRUE))) %>%
  relocate(OCJENA, .after = last_col())

MAT2_KOL_redovni <- podaci %>% filter(Status == "RED", OCJENA != -1) %>%
  unite("student", Ime, Prezime, sep = " ") %>%
  select(c(student, KOL1:KOL3, DZ, KVIZ, ESEJ)) %>%
  pivot_longer(c(KOL1:KOL3, DZ, KVIZ, ESEJ), names_to = "aktivnost", values_to = "bodovi") %>%
  mutate(aktivnost = factor(aktivnost, levels=c('KOL1', 'KOL2', 'KOL3', 'KVIZ', 'DZ', 'ESEJ')))

korelacije_MAT2 <- podaci %>% filter(Status == "RED", OCJENA != -1) %>% 
  select(c(KOL1, KOL2, KOL3, KVIZ, DZ, ESEJ))
testRes_MAT2 <- cor.mtest(korelacije_MAT2, conf.level = 0.975)
 
AS_MED_MAT2 <- MAT2_KOL_redovni %>% group_by(aktivnost) %>% 
   summarise(mean_value = mean(bodovi,na.rm=TRUE), median_value = median(bodovi,na.rm=TRUE))
 
MAT2_text <- data.frame(
  label_mean = rep("mean", 6),
  label_median = rep("median", 6),
  aktivnost = factor(c("KOL1", "KOL2", "KOL3", "KVIZ", "DZ", "ESEJ")),
  x_pos = c(18, 18, 18, 18, 9.25, 9.25),
  bullet_pos = c(17.5, 17.5, 17.5, 17.5, 9, 9)
)

MAT2_blank <- data.frame(
  aktivnost = factor(rep(c("KOL1", "KOL2", "KOL3", "KVIZ", "DZ", "ESEJ"), each=2)),
  x = c(0,20,0,20,0,20,0,20,0,10,0,10),
  y = 0
)

MAT2_usmeni_pad <- podaci %>% filter(str_detect(USMENI, "^Nije")) %>%
  unite("student", Ime, Prezime, sep = " ") %>%
  select(c(student, KOL1:KOL3, DZ, KVIZ, ESEJ)) %>%
  pivot_longer(c(KOL1:KOL3, DZ, KVIZ, ESEJ), names_to = "aktivnost", values_to = "bodovi") %>%
  mutate(aktivnost = factor(aktivnost, levels=c('KOL1', 'KOL2', 'KOL3', 'KVIZ', 'DZ', 'ESEJ')))

MAT2_usmeni_prolaz <- podaci %>% filter(str_detect(USMENI, "^Zad")) %>%
  unite("student", Ime, Prezime, sep = " ") %>%
  select(c(student, KOL1:KOL3, DZ, KVIZ, ESEJ)) %>%
  pivot_longer(c(KOL1:KOL3, DZ, KVIZ, ESEJ), names_to = "aktivnost", values_to = "bodovi") %>%
  mutate(aktivnost = factor(aktivnost, levels=c('KOL1', 'KOL2', 'KOL3', 'KVIZ', 'DZ', 'ESEJ')))

podaci_ESEJ <- podaci %>% filter(Status == "RED", OCJENA != -1) %>%
  mutate(Kategorija = case_when(
    UKUPNO < 40 ~ "UKUPNO < 40",
    UKUPNO < 50 ~ "40 \U2264 UKUPNO < 50",
    UKUPNO < 61 ~ "50 \U2264 UKUPNO < 61",
    .default = "UKUPNO \U2265 61"),
    Kategorija = factor(Kategorija, 
                        levels = c("UKUPNO < 40", "40 \U2264 UKUPNO < 50",
                                   "50 \U2264 UKUPNO < 61", "UKUPNO \U2265 61"))) %>% 
  select(Ime, Prezime, mail, ESEJ, Kategorija)

kviz <- read_csv("MAT2_2022_2023_kvizovi.csv") %>%
  right_join(podaci_ESEJ %>% select(mail, Kategorija), by = "mail")

esej_kategorije <- podaci_ESEJ %>% group_by(Kategorija) %>%
  summarize(`broj studenata` = n(), `pisalo esej` = sum(!is.na(ESEJ)))

nije_pisalo_esej <- podaci_ESEJ %>% count(ESEJ) %>% 
  filter(is.na(ESEJ)) %>% pull(n)
pisalo_esej <- sum(esej_kategorije$`pisalo esej`)

kviz_kategorije <- kviz %>% pivot_longer(DER:K13, names_to = "KVIZ", values_to = "value") %>%
  select(Kategorija, KVIZ, value) %>%
  mutate(KVIZ = fct_relevel(factor(KVIZ), c("K10", "K11", "K12", "K13"), after = Inf))
```

Popisi {data-navmenu="Sumarne analize"}
=======================================================================

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

### Svi studenti

```{r}
podaci %>% arrange(desc(Status), desc(UKUPNO), desc(KOL)) %>%
  mutate(id=row_number()) %>%
  relocate(id) %>%
  kbl(caption = "Svi studenti") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

### Kolokvirali

```{r}
podaci %>% filter(OCJENA > 1) %>% 
  arrange(desc(Status), desc(UKUPNO), desc(KOL)) %>%
  mutate(id=row_number()) %>%
  relocate(id) %>%
  kbl(caption = "Studenti koji su kolokvirali") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

### Pad na usmenom

```{r}
podaci %>% filter(str_detect(USMENI, "^Nije")) %>% 
  arrange(desc(Status), desc(UKUPNO), desc(KOL)) %>%
  mutate(id=row_number()) %>%
  relocate(id) %>%
  kbl(caption = "Studenti koji su pali na usmenom ispitu") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

Gustoće {data-navmenu="Sumarne analize"}
=======================================================================

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

### Funkcije gustoća po aktivnostima (redovni studenti)

```{r warning=FALSE, fig.width=17, fig.height=9}
ggplot(MAT2_KOL_redovni, aes(x=bodovi)) + 
  geom_histogram(aes(y=..density..), binwidth = 1, boundary=1, 
                 color="#ec8ae5", fill="#48d09b", alpha=0.7) +
  geom_density(alpha=.2, fill="yellow") + geom_blank(data=MAT2_blank, aes(x=x,y=y)) +
  scale_x_continuous(name = "bodovi") + 
  scale_y_continuous(limits = c(0, 0.25)) + guides(color = "none", fill = "none") +
  facet_wrap(vars(aktivnost), scales = "free_x") +
  geom_vline(data=AS_MED_MAT2, aes(xintercept=mean_value), color="blue", 
             linetype="dashed", size=0.5, alpha=0.6) +
  geom_vline(data=AS_MED_MAT2, aes(xintercept=median_value), color="red", 
             linetype="dashed", size=0.5, alpha=0.6) +
  geom_text(data = MAT2_text, mapping = aes(label = "\u2022", x = bullet_pos), 
            y = 0.24, color = "blue", size = 9) + 
  geom_text(data = MAT2_text, mapping = aes(label = label_mean, x = x_pos), 
            y = 0.24, hjust = 0) +
  geom_text(data = MAT2_text, mapping = aes(label = "\u2022", x = bullet_pos), 
            y = 0.22, color = "red", size = 9) +
  geom_text(data = MAT2_text, mapping = aes(label = label_median, x = x_pos), 
            y = 0.22, hjust = 0) +
  geom_rug(alpha=0.3)
```

Column {data-width=150}
-----------------------------------------------------------------------

### Broj studenata po statusu {data-height=100}

```{r warning=FALSE}
podaci %>% filter(OCJENA != -1) %>% 
  count(Status) %>% kbl() %>% kable_styling()
```

### Brojčane vrijednosti (redovni studenti) {data-height=200}

```{r warning=FALSE}
MAT2_KOL_redovni %>% group_by(aktivnost) %>%
  summarize(BROJ = sum(!is.na(bodovi)),
            MEAN = round(mean(bodovi, na.rm = TRUE), 2),
            MEDIAN = median(bodovi, na.rm = TRUE), 
            MIN = min(bodovi, na.rm = TRUE),
            MAX = max(bodovi, na.rm = TRUE)) %>%
  kbl() %>% kable_styling()
```


Violine {data-navmenu="Sumarne analize"}
=======================================================================

```{r warning=FALSE, fig.width=17, fig.height=9}
ggplot(MAT2_KOL_redovni, aes(x=aktivnost, y=bodovi, fill=aktivnost)) +
  geom_dotplot(binaxis='y', stackdir='center', fill='black', alpha=0.3, dostsize = 0.2) +
  geom_violin(trim=F, alpha=0.3) + geom_boxplot(width=0.05, alpha=0.8) + xlab('') +
  theme(axis.text.x=element_text(size=11), axis.text.y=element_text(size=11), legend.position='none') +
  facet_wrap(vars(aktivnost), scale = "free") +
  theme(axis.text.x=element_blank(), axis.ticks.x=element_blank())
```

Ukupni bodovi {data-navmenu="Sumarne analize"}
=======================================================================

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

### Frekvencije ukupnih bodova (redovni studenti)

```{r warning=FALSE, fig.width=10, fig.height=5}
ggplot(podaci %>% filter(Status == "RED", OCJENA != -1), aes(x=UKUPNO)) +
  geom_histogram(binwidth = 1, boundary=1, color="#ec8ae5", fill="#48d09b", alpha=0.7) +
  scale_x_continuous(name = "ukupni bodovi", breaks = seq(0,100,5), limits = c(0,100)) +
  scale_y_continuous(name = "broj studenata", breaks = seq(0,30,2)) +
  theme(panel.grid.minor = element_blank())
```

### Ocjene (redovni)

```{r warning=FALSE, fig.width=10, fig.height=5}
ggplot(podaci %>% filter(Status == "RED", OCJENA != -1), aes(x=factor(OCJENA))) +
  geom_bar(width=0.7, fill="steelblue") +
  geom_text(stat="count", aes(label=..count..), vjust=-0.5, nudge_x = -0.2, size = 5) +
  geom_text(aes( label = sprintf('(%s)', scales::percent(..prop..)), group = 1), stat= "count",
            vjust = -.5, nudge_x = 0.15, size = 5) +
  scale_x_discrete(name = "Ocjena") +
  scale_y_continuous(name = "broj studenata", breaks = seq(0,250,25), limits = c(0,250)) +
  theme(panel.grid.major.x = element_blank())
```

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

### Distribucija bodova

```{r warning=FALSE, fig.width=10, fig.height=7}
ggplot(podaci %>% filter(Status == "RED", OCJENA != -1), aes(x=UKUPNO)) +
  geom_histogram(aes(y=..density..), color="#ec8ae5", fill="#48d09b", alpha=0.5,
                 breaks = c(0,20,50,61,75,91,100)) +
  geom_vline(aes(xintercept=mean(UKUPNO)),color="blue", linetype="dashed", size=0.5, alpha=0.6) +
  geom_vline(aes(xintercept=median(UKUPNO)),color="red", linetype="dashed", size=0.5, alpha=0.6) +
  annotate(geom="point", x=90, y=0.035, size=2, shape=21, fill="red",color="red") +
  annotate(geom="text", x=91, y=0.035, label="median",size=3.5,hjust=0) +
  annotate(geom="point", x=90, y=0.033, size=2, shape=21, fill="blue",color="blue") +
  annotate(geom="text", x=91, y=0.033, label="mean",size=3.5,hjust=0) +
  geom_density(alpha=.2, fill="yellow") +
  geom_rug(alpha=0.3) +
  scale_x_continuous(name = "ukupni bodovi", breaks = seq(0,100,5), limits = c(0,100)) +
  theme(panel.grid.minor.x = element_blank())
```

Korelacije {data-navmenu="Sumarne analize"}
=======================================================================

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

### Korelacije

```{r warning=FALSE, fig.width=10, fig.height=7}
corrplot(round(cor(korelacije_MAT2, use = "pairwise.complete.obs"), 2), type = "lower",
         diag=FALSE, addCoef.col = 'black', tl.srt = 45)
```

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

### p-vrijednosti korelacija

```{r warning=FALSE, fig.width=10, fig.height=7}
corrplot(round(cor(korelacije_MAT2, use = "pairwise.complete.obs"), 2), type = "lower",
         diag=FALSE, tl.srt = 45, p.mat = testRes_MAT2$p, insig = 'p-value', sig.level = -1)
```

Usmeni - grafički prikaz  {data-navmenu="Analiza usmenog"}
=======================================================================

### Usmeni (grafički prikaz)

```{r fig.height = 6}
ggplot(podaci %>% filter(!is.na(USMENI)), aes(x = factor(USMENI))) +
  geom_bar(fill="steelblue") +
  geom_text(stat="count", aes(label=..count..), vjust=-0.5, nudge_x = -0.1, size = 5) +
  geom_text(aes( label = sprintf('(%s)', scales::percent(..prop..)), group = 1), stat= "count",
            vjust = -.5, nudge_x = 0.1, size = 5) +
  xlab("") + ylab("") +
  scale_y_continuous(breaks = seq(0,100,25), limits = c(0,105)) 
```

Pad i prolaz na usmenom {data-navmenu="Analiza usmenog"}
=======================================================================

## Column 1

### Frekvencije bodova po aktivnostima studenata koji su pali na usmenom

```{r warning=FALSE}
ggplot(MAT2_usmeni_pad, aes(x=bodovi)) + 
  geom_histogram(binwidth = 1, boundary=1, color="#ec8ae5", fill="#48d09b", alpha=0.7) +
  geom_blank(data=MAT2_blank, aes(x=x,y=y)) +
  facet_wrap(vars(aktivnost), scale = "free") + xlab("") + ylab("")
```

## Column 2

### Frekvencije bodova po aktivnostima studenata koji su prošli na usmenom

```{r warning=FALSE}
ggplot(MAT2_usmeni_prolaz, aes(x=bodovi)) + 
  geom_histogram(binwidth = 1, boundary=1, color="#ec8ae5", fill="#48d09b", alpha=0.7) +
  geom_blank(data=MAT2_blank, aes(x=x,y=y)) +
  facet_wrap(vars(aktivnost), scale = "free") + xlab("") + ylab("")
```

Esej po kategorijama {data-navmenu="Dodatne analize"}
=======================================================================

## Column 1 {data-width=200}

### Frekvencije bodova na eseju prema ukupnim bodovima

```{r warning=FALSE, fig.width=8}
ggplot(podaci_ESEJ, aes(x=ESEJ)) + 
  geom_histogram(binwidth = 1, boundary=1, color="#ec8ae5", fill="#48d09b", alpha=0.7) +
  scale_x_continuous(breaks = seq(0,10,1), limits = c(0,10)) + xlab("") + ylab("") +
  facet_wrap(vars(Kategorija))
```

## Column 2 {data-width=70}

### Broj redovnih studenata koji su pisali esej (po kategorijama) {data-height=100}

```{r warning=FALSE}
esej_kategorije %>% kbl() %>% kable_styling()
```

### Esej (ukupno u sumi za redovne studente) {data-height=50}

```{r warning=FALSE}
tibble(`pisalo esej` = pisalo_esej, 
       `nije pisalo esej` = nije_pisalo_esej) %>% 
  kbl() %>% kable_styling()
```

### Esej (ukupno u sumi za redovne studente u postocima) {data-height=50}

```{r warning=FALSE}
tibble(`pisalo esej` = scales::percent(round(pisalo_esej / (pisalo_esej + nije_pisalo_esej),2)), 
       `nije pisalo esej` = scales::percent(round(nije_pisalo_esej / (pisalo_esej + nije_pisalo_esej),2))) %>% 
  kbl() %>% kable_styling()
```

Pojedinačni kvizovi {data-navmenu="Dodatne analize"}
=======================================================================

### Frekvencije bodova po kvizovima

```{r warning=FALSE, fig.width=10}
ggplot(kviz_kategorije, aes(x = value)) +
  geom_histogram(fill="steelblue") + 
  facet_wrap(vars(KVIZ), scales = "free", nrow = 2) +
  theme(strip.text = element_text(size = 5),
        axis.text.x = element_text(size = 5),
        axis.text.y = element_text(size = 6)) + xlab("") + ylab("")
```

Pojedinačni kvizovi po kategorijama {data-navmenu="Dodatne analize"}
=======================================================================

### Frekvencije bodova po kvizovima i kategorijama

```{r warning=FALSE, fig.width=10}
ggplot(kviz_kategorije, aes(x = value)) +
  geom_histogram(fill="steelblue") + 
  facet_grid(rows = vars(Kategorija), cols = vars(KVIZ), scales = "free") +
  theme(strip.text = element_text(size = 5),
        axis.text.x = element_text(size = 5, angle = 90),
        axis.text.y = element_text(size = 6)) + xlab("") + ylab("")
```

Pristupanje kvizovima {data-navmenu="Dodatne analize"}
=======================================================================

## Column 1 {.tabset .tabset-fade}

### Općenito

```{r warning=FALSE, fig.height=8, fig.width=8}
vis_miss(kviz %>% select(DER:K13))
```

### Varijable

```{r warning=FALSE, fig.height=8, fig.width=8}
gg_miss_var(kviz %>% select(DER:K13))
```

### Studenti

```{r warning=FALSE, fig.width=8, fig.height=8}
gg_miss_case(kviz %>% select(DER:K13))
```

### Varijable (kategorije u %)

```{r warning=FALSE, fig.width=8, fig.height=8}
gg_miss_fct(kviz %>% select(DER:Kategorija), fct = Kategorija)
```

## Column 2 {.tabset .tabset-fade}

### Općenito (klasterirano)

```{r warning=FALSE, fig.height=8, fig.width=8}
vis_miss(kviz %>% select(DER:K13), cluster = TRUE)
```

### Varijable (kategorije)

```{r warning=FALSE, fig.width=8, fig.height=8}
gg_miss_var(kviz %>% select(DER:Kategorija), facet = Kategorija)
```

### Studenti (kategorije)

```{r warning=FALSE, fig.width=8, fig.height=8}
gg_miss_case(kviz %>% select(DER:Kategorija), facet = Kategorija)
```

Pristupanje kvizovima - presječni pogled {data-navmenu="Dodatne analize"}
=======================================================================

### Pristupanje kvizovima - presječni pogled

```{r warning=FALSE, fig.height=8, fig.width=16}
gg_miss_upset(kviz %>% select(DER:K13), nset = 14, nintersect = 50)
```

