Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Practicals/Hsuan/Assignment 1/My Info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am from Taiwan, the pronounciation of my name is the same as Shuan.
76 changes: 76 additions & 0 deletions Practicals/Hsuan/Exercise 1/Exercise_1_HsuanLee.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
title: "Exercise 1"
author: "Hsuan Lee"
output:
html_document:
css: style.css
---

**8 Monte Carlo Simulation Exercise**

Question a and b

```{r}
set.seed(9252568)

# n.sample is number of samples that we wanna sample
# size is the sample size of each sample
# mean is the sample mean of each sample that we sampled form
# sd is the sample sd of each sample that we sampled from

sample_samples <- function(n.sample, size, mean, sd){

# create the matrix to store each values in each sample
samples <- matrix(0, nrow = size, ncol = n.sample)
colnames(samples) <- c(1:n.sample)

# create the matrix to store means, absolute bias, SE, 95% CI, sample bias for each sample
info_sample <- data.frame(matrix(0, nrow = n.sample, ncol = 6))
colnames(info_sample) <- c("means", "abs_bias", "SE", "low_95_CI", "high_95_CI",
"sample_bias")

# sample samples
for (i in 1:n.sample) {
samples[,i] <- rnorm(n = size, mean = mean, sd = sd)
}

# display the info of each sample
for (i in 1:n.sample) {
info_sample[i,1] <- mean(samples[,i])
info_sample[i,2] <- abs(info_sample[i,1] - mean)
info_sample[i,3] <- sd(samples[,i]) / sqrt(size)
info_sample[i,4] <- mean(samples[,i]) - 1.96*info_sample[i,3]
info_sample[i,5] <- mean(samples[,i]) + 1.96*info_sample[i,3]
info_sample[i,6] <- ifelse((info_sample[i,4] > mean) | (info_sample[i,5] < mean),
"Biased", "Correct")
}

return(info_sample)
}

# draw the samples
my_draw <- sample_samples(100, 1000, 2, 0.8)
```

Question c

```{r, results='hide', message=F, warning=FALSE}
library(tidyverse)
```

```{r, warning = FALSE, message = FALSE, error = FALSE, results='hide', fig.keep = 'all', out.width = "100%", fig.align = 'center'}
ggplot(data = my_draw, aes(x = 1:100, y = means)) +
geom_point(color = "#585858") +
geom_errorbar(aes(ymin = low_95_CI, ymax = high_95_CI, colour = sample_bias)) +
scale_color_manual(values=c("Blue", "#383838"), name = "Biased Sample", ) +
geom_hline(yintercept = 2, color = "Red", linetype = "dashed") +
labs(x="Simulations", y = "95% Confident Interval") +
theme_classic()
```

Question d

```{r}
my_draw %>% filter(sample_bias == "Biased")
```

284 changes: 284 additions & 0 deletions Practicals/Hsuan/Exercise 1/Exercise_1_HsuanLee.html

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions Practicals/Hsuan/Exercise 1/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* Whole document: */
body{
font-family: Times New Roman;
font-size: 16pt;
}
/* Headers */
h1{
text-align: center;
font-size: 20pt;
font-weight: bold;
}
h2,h3,h4,h5,h6{
text-align: center;
font-size: 16pt;}
72 changes: 72 additions & 0 deletions Practicals/Hsuan/Exercise 2/HsuanLee_Exercise_2.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
title: "Exercise 2"
author: "Hsuan Lee"
output: html_document
---

## Introduction

The study is concerned with whether the number of rat sighting is tied to the population of a city and the waste of food thrown on the streets.


The data analyzed are simulated with 4 variables, they are:

*city_id*: ID number of each city
*popu*: population of a city in millions.
*was_food*: the amount of food thrown on the street an year, in tons
*rat_sight*: the number of rat sighting an year

```{r}
library(truncnorm) # for function rtruncnorm() that allow to set the max and min for normal dist
options(scipen = 999)
```


**Simulate the data**
```{r}
set.seed(9252568)
city_id <- c(1:2000)
popu <- rtruncnorm(n = 2000, a = 0.05, mean = 1, sd = 1)
was_food <- rtruncnorm(2000, a = 10, mean = 30, sd = 20)

rat_sight <- round(700.5 + 8.7*popu + 12.7*was_food + rnorm(2000, mean = 0.5),1)

rat <- data.frame(city_id, popu, was_food, rat_sight)
```

**Analysis**
```{r}
# operate the linear regression for analyzing
rat_model <- lm(rat_sight~ popu + was_food, data = rat)

summary(rat_model)
```

## Replication

**Simulation**
```{r}
set.seed(92525568)

city_id <- c(1:2000)
popu <- rtruncnorm(n = 2000, a = 0.05, mean = 1, sd = 1)
was_food <- rtruncnorm(2000, a = 10, mean = 30, sd = 20)

rat_sight <- round(700.5 + 8.7*popu + 12.7*was_food + rnorm(2000, mean = 0.5),1)

rat_rep <- data.frame(city_id, popu, was_food, rat_sight)
```

**Analysis**
```{r}
rep <- lm(rat_sight~ popu + was_food, data = rat_rep)
summary(rep)
```

The results are quite similar after changing the seed.

```{r}
sessionInfo()
```


527 changes: 527 additions & 0 deletions Practicals/Hsuan/Exercise 2/HsuanLee_Exercise_2.html

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions Practicals/Hsuan/Exercise 3/HsuanLee_Exercise3.aux
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
\relax
\providecommand\hyper@newdestlabel[2]{}
\providecommand\HyperFirstAtBeginDocument{\AtBeginDocument}
\HyperFirstAtBeginDocument{\ifx\hyper@anchor\@undefined
\global\let\oldnewlabel\newlabel
\gdef\newlabel#1#2{\newlabelxx{#1}#2}
\gdef\newlabelxx#1#2#3#4#5#6{\oldnewlabel{#1}{{#2}{#3}}}
\AtEndDocument{\ifx\hyper@anchor\@undefined
\let\newlabel\oldnewlabel
\fi}
\fi}
\global\let\hyper@last\relax
\gdef\HyperFirstAtBeginDocument#1{#1}
\providecommand\HyField@AuxAddToFields[1]{}
\providecommand\HyField@AuxAddToCoFields[2]{}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{1}{1/1}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {1}{1}}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{2}{2/2}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {2}{2}}}
\newlabel{working<1>}{{3}{3}{}{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {working<1>}{3}}
\newlabel{working}{{3}{3}{}{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {working}{3}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{3}{3/3}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {3}{3}}}
\newlabel{aligning<1>}{{4}{4}{}{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {aligning<1>}{4}}
\newlabel{aligning}{{4}{4}{}{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {aligning}{4}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{4}{4/4}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {4}{4}}}
\newlabel{omit<1>}{{5}{5}{}{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {omit<1>}{5}}
\newlabel{omit}{{5}{5}{}{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {omit}{5}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{5}{5/5}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {5}{5}}}
\newlabel{ugly<1>}{{6}{6}{}{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {ugly<1>}{6}}
\newlabel{ugly}{{6}{6}{}{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {ugly}{6}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{6}{6/6}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {6}{6}}}
\newlabel{discussion<1>}{{7}{7}{}{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {discussion<1>}{7}}
\newlabel{discussion}{{7}{7}{}{Doc-Start}{}}
\@writefile{snm}{\beamer@slide {discussion}{7}}
\@writefile{nav}{\headcommand {\slideentry {0}{0}{7}{7/7}{}{0}}}
\@writefile{nav}{\headcommand {\beamer@framepages {7}{7}}}
\@writefile{nav}{\headcommand {\beamer@partpages {1}{7}}}
\@writefile{nav}{\headcommand {\beamer@subsectionpages {1}{7}}}
\@writefile{nav}{\headcommand {\beamer@sectionpages {1}{7}}}
\@writefile{nav}{\headcommand {\beamer@documentpages {7}}}
\@writefile{nav}{\headcommand {\gdef \inserttotalframenumber {7}}}
\gdef \@abspage@last{7}
Loading