Lab1 Probability PDF

Title Lab1 Probability
Author pranav bajpai
Course Probability and Statistical Inference
Institution Duke University
Pages 11
File Size 398.9 KB
File Type PDF
Total Downloads 43
Total Views 150

Summary

Lab1...


Description

Lab 1: Introduction to R & RStudio Pranav Bajpai 01/10/2020 Objective The purpose of this lab is to introduce you to the basics of computing in this course using R and RStudio.

Lab Procedures Loading Packages Welcome to RStudio in the Cloud! R is a free and open-source programming language widely used among statisticians for data analysis and visualization, and RStudio provides a nice front-end environment for R. “In the Cloud” means that you won’t need to download anything to your own machine, and you can access RStudio from any web browser by going to https://rstudio.cloud. Before we get started we need to load a few packages. R is an open-source programming language, meaning that users can contribute packages that make our lives easier and we can use them for free. In this lab we’ll use the three packages below. • dplyr: for data wrangling • ggplot2: for data visualization • readr: for loading data We load packages into the working environment using the code below. library(dplyr) library(ggplot2) library(readr)

Getting Started Let’s jump right in and start by demonstrating what R is capable of! Start by clicking the button above labeled “Knit”. After a few seconds, a pdf document should pop up. Compare the new pdf with the original document with the suffix .Rmd (this document). What do you notice? What are connections between the original file and the knitted pdf? Can you think of rules that get from one to the other? Chat with a neighbor and ask the TAs if you have any questions. We just demonstrated something powerful. This file is called an R Markdown (.Rmd) file. This file type enforces reproducibility, meaning that anyone is able to run your code and receive the same results you did. It also allows you to generate high-quality reports that include text, code, output, and figures in a single document (do you see examples of all of these in your pdf file?) All lab reports in this course (using R) will be created using R Markdown.

1

RStudio Basics Your RStudio window has four panels. Your R Markdown file (this document) is in the upper-left panel. The panel on the lower left is where the action happens. It’s called the console. Everytime you launch RStudio, it will have the same text at the top of the console telling you the version of R that you’re running. Below that information is the prompt. As its name suggests, this prompt is a request for a command. The panel in the upper right contains your workspace as well as a history of the commands that you’ve previously entered. Any plots that you generate will show up in the panel in the lower right corner. This is also where you can browse your files, access help, manage packages, etc. Let’s start by writing our first line of code in R in the console (lower-left). Put your cursor next to the “>” and type 2+2, then hit enter. You should see the expected result (4) and should also notice that the History (upper-right) panel shows 2+2 as well. We can do more complicated expressions as well. Try typing log(exp(15)) + (6/2) * 3ˆ2. Does R give the expected result below the code? We used the Console as a calculator to do 2+2, but it is tricky to edit that code or try something new, and it doesn’t enforce reproducibility. When you compared the R Markdown file to the knitted pdf, you probably noticed some greyed out portions. These are called code chunks. You can add a code chunk to a file by typing OPTION + COMMAND + I on a Mac or CONTROL + ALT + I on Windows. Or just type the characters below. Note the “grave accent” character is below the ESC key on most keyboards. 2 + 2 ## [1] 4 log(exp(15)) + (6/2) * 3^2 ## [1] 42 To run a code chunk click on the green arrow at the top of the code chunk in the R Markdown file or highlight the lines and click “Run” in the upper-right corner of this pane. You can also put your cursor on a line of code and do COMMAND + OPTION on a Mac or CONTROL + ENTER on Windows. Try this now. Dr. Arbuthnot’s Baptism Records Now let’s work with some real data. To get you started, run the following command to read in the data. Notice that we can add comments to code chunks using a pound sign. # read in the data arbuthnot...


Similar Free PDFs