R Course 5th lecture.

Slides:



Advertisements
Similar presentations
Introduction to R Graphics
Advertisements

Graphics in R data analysis and visualization Katia Oleinik Scientific Computing and Visualization Boston University
Advanced Higher STATISTICS Linear Regression To see if there is a relationship between two variables, we draw a scatter-graph. It is then possible to draw.
R graphics  R has several graphics packages  The plotting functions are quick and easy to use  We will cover:  Bar charts – frequency, proportion 
SCATTER PLOTS AND SMOOTHING. An Example – Car Stopping Distances An experiment was conducted to measure how the stopping distance of a car depends on.
4.1 System of linear Equations Solving graphically Solving by substitution Solving by addition.
SW318 Social Work Statistics Slide 1 Using SPSS for Graphic Presentation  Various Graphics in SPSS  Pie chart  Bar chart  Histogram  Area chart 
Assumption of Homoscedasticity
Correlation in SPSS. Correlation in SPSS is very simple and efficient  Step 1. Switch to Data View to make sure the data isn’t corrupted or changed.
Graphing Linear Equations. What is a Linear Equation? A linear equation is an equation whose graph is a LINE. Linear Not Linear.
Section 2.3 Graphs of Linear Equations in Two Variables.
R-Graphics Day 2 Stephen Opiyo. Basic Graphs One of the main reasons data analysts turn to R is for its strong graphic capabilities. R generates publication-ready.
An introduction to R: get familiar with R Guangxu Liu Bio7932.
Ch2: Exploring Data: Charts 13 Sep 2011 BUSI275 Dr. Sean Ho HW1 due Thu 10pm Download and open “SportsShoes.xls”SportsShoes.xls.
2.8Exploring Data: Quadratic Models Students will classify scatter plots. Students will use scatter plots and a graphing utility to find quadratic models.
Scatter plots and Regression Algebra II. Linear Regression  Linear regression is the relationship between two variables when the equation is linear.
Equations of Linear Relationships
Statistics with TI-Nspire™ Technology Module E. Lesson 2: Properties Statistics with TI-Nspire™ Technology Module E.
Scatter Diagrams Objective: Draw and interpret scatter diagrams. Distinguish between linear and nonlinear relations. Use a graphing utility to find the.
R-Graphics Stephen Opiyo. Basic Graphs One of the main reasons data analysts turn to R is for its strong graphic capabilities. R generates publication-ready.
GRAPHING A “PICTURE” OF THE RELATIONSHIP BETWEEN THE INDEPENDENT AND DEPENDENT VARIABLES.
Review Lecture 51 Tue, Dec 13, Chapter 1 Sections 1.1 – 1.4. Sections 1.1 – 1.4. Be familiar with the language and principles of hypothesis testing.
Ggplot2 A cool way for creating plots in R Maria Novosolov.
Plotting Complex Figures Using R
Data Science and Big Data Analytics Chap 3: Data Analytics Using R
Introduction to Graphics in R 3/12/2014. First, let’s get some data Load the Duncan dataset It’s in the car package. Remember how to get it? – library(car)
Regression Analysis in Microsoft Excel MS&T Physics 1135 and 2135 Labs.
1 Introduction to R A Language and Environment for Statistical Computing, Graphics & Bioinformatics Introduction to R Lecture 3
Statistical Programming Using the R Language Lecture 2 Basic Concepts II Darren J. Fitzpatrick, Ph.D April 2016.
Lecture 2 Functions Trevor A. Branch FISH 553 Advanced R School of Aquatic and Fishery Sciences University of Washington.
Linear Regression:. The relationship between two variables (e.g. height and weight; age and IQ) can be described graphically with a scatterplot : shortmediumlong.
Creating a picture of your data
Copyright 2013, 2010, 2007, 2005, Pearson, Education, Inc.
CLOSE Please YOUR LAPTOPS, and get out your note-taking materials.
Using Excel to Construct Basic Tools for Understanding Variation
Linear Equation in Two Variables
Using R Graphs in R.
Unit 3: Visualizing the Change Section 5: Starting from Over There
Computer Application in Engineering Design
Standard and Slope-Intercept Form
Statistical Programming Using the R Language
Graphing.
Summary Statistics in R Commander
Warm UP Write down objective and homework in agenda
R Assignment #4: Making Plots with R (Due – by ) BIOL
Microsoft Office 2013 Coming to a PC near you!.
CHAPTER 10 Correlation and Regression (Objectives)
Module 12 Math 075.
Edexcel: Large Data Set Activities
Polynomial and Rational Functions
Graphs in Science Chapter 2 Section 3.
INTRODUCTION TO SGPLOT Zahir Raihan OVERVIEW  ODS Graphics  SGPLOT overview  Plot Content  High value plot statements  High value plot options 
(& Generalized Linear Models)
Graphing Linear Equations
Graphs with SPSS.
Data Analysis and Statistics
Discrete Least Squares Approximation
7.1 Draw Scatter Plots & Best-Fitting Lines
Lesson 13.1 How do you find the probability of an event?
Lecture 6 Re-expressing Data: It’s Easier Than You Think
Graphing.
R course 6th lecture.
3 Chapter Chapter 2 Graphing.
R course 4th lecture.
Graphing & Statistics Honors Biology.
Creating a Graph With Microsoft Excel
R Course 5th lecture.
GRADIENTS AND STRAIGHT LINE GRAPHS
Practice Important Formulas
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

R Course 5th lecture

Task Create a function which removes even numbers from a vector Hint: Use modulus and vector subsetting

Easiest solution is just removeeven<- function(x){ x[x%%2 != 0] }

Can also use while and if removeeven<-function(x){ i<-length(x) while(i>0){ if(x[i]%%2==0){ x<-x[-i] } i<-i-1 x

Can also use while and if removeeven<-function(x){ i<-length(x) while(i>0){ if(x[i]%%2==0){ x<-x[-i] } i<-i-1 x

Could also use ceiling instead of modulus removeeven<-function(x){ i<-length(x) while(i>0){ if(ceiling(x[i]/2)==x[i]/2){ x<-x[-i] } i<-i-1 x

Create a function printing the first 10 fibonacci numbers Hint: will probably need to use subsetting and a for loop

Solution (can be done in several other ways) fibonacci<-function(){ x<-1 y<-c(1) for(i in 1:9){ y[i+1]<-x x<-y[i+1]+y[i] } y

lapply() lapply() is a useful function in R, that allows you to apply a function that takes one input to a vector, and outputs a vector lapply(x,function,parameters) where x is a list/vector

Task Create a function that squares all odd numbers in a vector Hint: create a nested function to square odd inputs and use lapply(x,function), which applies a function to a vector x

squareodd<-function(x){ ifodd<-function(y){ if(y%%2!=0){ y<-y^2 } y t(lapply(x,ifodd))

Task Create a function to find the number of ways of selecting k objects from n when the order of the selection does matter Create a function to find the number of ways when order doesn’t matter Hint: Use factorial() in R for factorials

Hint The formula for permutations is n!/(n-k)! The formula for combinations is n!/k!(n-k)!

Plotting graphics Many functions to plot graphics plot() - data represented differently depending on data types hist() - histogram barplot() - bar plot boxplot() - box plot pairs() - produces a matrix of scatterplots

Customising plots You can add arguments such as col="red",pch=4,main="title",xlab="xtitle",ylab="ytitle" See help(plot), help(“col"), or help(“pch") for more options or look online etc. Can use attach() to attach loaded datasets so can just use variable names E.g. attach(cars) allows you to use speed and dist, or can use cars$speed / cars$dist View(cars) shows you the dataset

pch point types

Example using cars dataset attach(cars) plot(cars) or plot(speed,dist) plot(cars,col="red",pch=4) hist(speed)

Adding regression lines You can create a linear model of a dataset using lm() This will give you the intercepts of the data You can then use abline(lm()) to add a fitted line to the plotted data You can also use fitted(lm()) to produce the fitted values of the corresponding x values

Regression using cars data carsregr<-lm(dist~speed,data=cars) plot(cars,col="red",pch=4) abline(carsregr,col="blue")

Boxplots and pairs attach(mtcars) boxplot(disp~cyl) plots a boxplot of disp given cyl pairs(~mpg+disp+drat+wt,data=mtcars,main="scatterplot matrix") Can also colour by category plot(mpg,disp,col=as.numeric(as.factor(disp)),pch=4)

Task Use the default dataset mtcars, and create a scatterplot cylinders versus miles per gallon, labelling the axes and graph and adding titles, make it look more aesthetically pleasing and easy to read (choose clear point styles and colours), and plot a line of best fit

Create a histogram of the default dataset AirPassengers You can customise drawing using limits, breaks etc. (use help for options) Can also add density function with lines(density(AirPassengers))

Use state. x77 dataset to plot population vs area and to calculate pop Use state.x77 dataset to plot population vs area and to calculate pop. density Also compare life exp. Vs. murder rates (plot points, create a linear model, plot fitted line etc.) Compare illiteracy vs. HS grad rates