Descriptive Statistics

Row

Interactive Distribution of Plant Growth

Interactive MPG vs Weight

t-tests

Row

Group Comparisons (Independent t-test)

ANOVA

Row

One-Way ANOVA: Group Variance

Chi-Squared

Row

Proportional Breakdown (Cylinders by Gear)

Correlation

Row

Heatmap of mtcars Correlations

Linear Regression

Row

Simple Regression: Observed vs Predicted

Multiple Regression: Observed vs Predicted

---
title: "Interactive Statistical Dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source_code: embed
    self_contained: true
    vertical_layout: fill
    theme: flatly
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(datarium)
library(rstatix)
library(plotly) 
library(ggpubr)
library(corrplot)

# Data Preparation
data("PlantGrowth")
data("selfesteem")
data("mtcars")

# Long format for RM-ANOVA
selfesteem_long <- selfesteem %>%
  gather(key = "time", value = "score", t1, t2, t3) %>%
  convert_as_factor(id, time)

```

# Sidebar {.sidebar}

### 📖 Data Dictionary

**PlantGrowth**

-   `weight`: Dry yield of plants.
-   `group`: Treatment condition (ctrl, trt1, trt2).

**mtcars**

-   `mpg`: Miles per gallon.
-   `wt`: Weight (1000 lbs).
-   `hp`: Gross horsepower.

**selfesteem**

-   `t1/t2/t3`: Self-esteem scores over time.

------------------------------------------------------------------------

### 🎓 Acknowledgments

\*My sincere thanks to everyone who attended the dashboard webinar today. Your participation and eagerness to learn were greatly appreciated.

🎄 **Merry Christmas to you all**💐

# Descriptive Statistics

## Row

### Interactive Distribution of Plant Growth

```{r}
p1 <- ggplot(PlantGrowth, aes(x=weight, fill=group)) +
  geom_density(alpha=0.5) +
  theme_minimal()

ggplotly(p1)

```

### Interactive MPG vs Weight

```{r}
library(ggplot2)
library(ggforce)

P2 <- ggplot(mtcars, 
            aes(wt, mpg)) +
  geom_point(aes(color = factor(cyl), 
                 shape = factor(cyl)), size = 2) +
  ggtitle("Iris flowers with Confidence Ellipses") +
  theme_classic(14) +
  theme(plot.title = element_text(hjust = 0.5, face = "bold", color = "darkblue"))

# Add confidence ellipses
P2 + stat_ellipse(aes(color = factor(cyl)), level = 0.95)

```

# t-tests

## Row

### Group Comparisons (Independent t-test)

```{r}
plant_sub <- PlantGrowth %>% filter(group %in% c("ctrl", "trt1"))

p3 <- ggplot(plant_sub, aes(x=group, y=weight, fill=group)) +
  geom_boxplot() +
  geom_jitter(width=0.1) +
  theme_light()

ggplotly(p3)

```

# ANOVA

## Row

### One-Way ANOVA: Group Variance

```{r}
p4 <- ggplot(PlantGrowth, aes(x=group, y=weight, fill=group)) +
  geom_violin(alpha=0.3) +
  geom_boxplot(width=0.1) +
  theme_minimal()

ggplotly(p4)

```

### Repeated Measures: Self-Esteem Trends

```{r}
# Visualizing change over time
p5 <- ggplot(selfesteem_long, aes(x=time, y=score, group=id, color=id)) +
  geom_line() +
  geom_point() +
  theme_bw() +
  labs(title = "Individual Subject Progress")

ggplotly(p5)

```

# Chi-Squared

## Row

### Proportional Breakdown (Cylinders by Gear)

```{r}
p6 <- ggplot(mtcars, aes(x=factor(gear), fill=factor(cyl))) +
  geom_bar(position="fill") +
  labs(x="Gears", y="Proportion") +
  theme_minimal()

ggplotly(p6)

```

# Correlation

## Row

### Heatmap of mtcars Correlations

```{r}
cor_mat <- round(cor(mtcars[,1:7]), 2)
plot_ly(z = cor_mat, 
        x = colnames(cor_mat), 
        y = rownames(cor_mat), 
        type = "heatmap", 
        colorscale = "RdBu")

```

# Linear Regression

## Row

### Simple Regression: Observed vs Predicted

```{r}
model <- lm(mpg ~ wt + hp, data = mtcars)
library(ggeffects)
ggeffect(model)|> 
  plot() |> 
  sjPlot::plot_grid()

```


### Multiple Regression: Observed vs Predicted

```{r}
model <- lm(mpg ~ wt + hp, data = mtcars)
mtcars$predicted <- predict(model)

library(plotly)
p8<- plot_ly(data = mtcars, 
               x = ~wt, 
               y = ~mpg, 
               z = ~hp, 
               color = ~factor(cyl),  
               type = "scatter3d",
               mode = "markers",
               marker = list(size = 5)) # Adjust marker size

# Add a title and axis labels
p8 <- p8 %>% layout(title = "3D car Scatter Plot",
                      scene = list(xaxis = list(title = 'Weight (1000 lbs)'),
                                   yaxis = list(title = 'Miles/(US) gallon'),
                                   zaxis = list(title = 'Gross horsepower')))

# Display the plot
ggplotly(p8)

```