--- title: "Lab01: Practice Code" author: "Kenya Amano" date: "10/2/2020" output: html_document: df_print: paged pdf_document: default editor_options: chunk_output_type: console --- {r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)  # Prerequiste {r, message=FALSE} rm(list = ls()) # Clear memory library(tidyverse) # Load package  # Vector Practice 1. Vector1 : The numbers one through five and then the number six five times 2. Vector2 : 10 randomly drawn numbers from a normal distribution with a mean 10 and a s.d. of 1 3. Vector3 : Results of 10 single binomial trials with a probability of 0.4 4. Vector4 : Sample 100 observations from a 5-trial binomial distribution with a probability of success of 0.4 5. Vector5 : The numbers one through three and the word apple {r}  6. What type of data is Vector2? 7. Round up Vector2 to two decimal place 8. What happened in Vector5? {r}  # Matrices Practice 1. Matrix1: Create 5 by 5 matrix containing all NAs 2. Assign Matrix1 the row names (a,b,c,d,e) and the column names (1,2,3,4,5) 3. Replace the NAs in the first columne of Matrix1 with "Inf" {r}  # List Practice 1. Create a list that contains Vector1, Vector2, Vector3, and Matrix1 2. Name each list component as Vector1, Vector2, Vector3, and Matrix1 respectively 3. Locate Vector2 from the list {r}  # Data Frames Practice 1 # Working directory Check if your working directory is correct (where you have saved Lab01Data.csv and Lab01Survey.csv) {r} basedir <- getwd() rowdata.folder <- paste(basedir, "Specify if you create a folder", sep = "/")  ## 1. Load Lab01data.csv in R {r} # Load data in simple way Dta <- read.csv("Lab01Data.csv", header = TRUE, stringsAsFactors = FALSE) # Specify the folder Dta <- read.csv(paste(rowdata.folder, "Lab01Data.csv", sep="/")) # Directly download from website DataURL <- "http://staff.washington.edu/kamano/MLE/LabMaterials/Lab01/Lab01Data.csv" Dta <- read.csv(DataURL)  ## 2. What is the data structure? What does that tell us about type? {r} # Check structure  ## 3. Check the names and summary statistics of the data. Fix any names that are less than good. {r} # Check and fix names  ## 4. Remove observations with missing values {r}  ## 5. Calculate the average GDP per capita for Brazil for the observed period. Repeat the calculation for all countries. {r}  ## 6. Plot GDP per capita (on the x-axis) and Polity2 (on the y-axis) {r}  ## 7. Create a new variable called "democracy". Assign 0 to countries with negative value or zero polity2 score, and assign 1 to countries with positive score. {r, results='hide'}  # Data Frames Practice 2 ## 1. Read in the data "lab1_survey.csv" {r} # Clear and load data rm(list = ls()) SurveyData <- read.csv(file = "Lab01Survey.csv") # or DataURL2 <- "http://staff.washington.edu/kamano/MLE/LabMaterials/Lab01/Lab01Survey.csv" SurveyData <- read.csv(DataURL2)  ## 2. Inspect the data. What format are they in? What values do the data take, and how do those values correspond with the survey? {r}  ## 3. Generate some summary statistics. {r}  ## 4. How are these two variables related to each other (assuming equal intervals b/w categories)? {r}  The correlation b/w R knowledge and LaTeX knowledge is r cor1, or more nicely, r round(cor1, 2). ## 5. Are there any problems with the way the data are coded? (Think about lecture yesterday.) ## 6. Recode the data {r}  ## 7. Why is this coding method better? ## 8. Generate some plots of the data: bar charts are good here, scatterplots even better. {r, echo= FALSE}  # LaTex in R Markdown $$1 + 1 = 2$$ $$11 \times 11 = 121 \\$$ $$E = mc^2$$ I think it's Einstein who proposed$E = mc^2\$. $$x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$$ $$\begin{split} X & = (x+a)(x-b) \\ & = x(x-b) + a(x-b) \\ & = x^2 + x(a-b) - ab \end{split}$$ # Install guide for Chris's packages and TinyTeX {r} # Install tile install.packages("https://faculty.washington.edu/cadolph/software/tile_0.4.15.tar.gz", repos = NULL, type="source") # Install simcf install.packages("https://faculty.washington.edu/cadolph/software/simcf_0.2.18.tar.gz", repos = NULL, type="source")  # Install tinytex ## Caution!!!! It takes sooooooooooooo long!! ## {r} install.packages("tinytex") tinytex::install_tinytex()