#ECO239 R Introduction on Oct. 22, 2015# #Use it as calculator #Operators # Add numbers from 1 to 3 1+2+3 # Multiplication 1*2*3 # Devision 5/2 # Powers, ** and ^ return the same result 2**3 2^3 # square root sqrt(2) # Natural log, ln 2 log(2) # ln(pi) pi log(pi) # Absolute value abs(3-5) # Exponential exp(3) # Which operation is first? 4*(3+2) # And Parenthesis?? (4+5)/3 # Variables, assigning a value to a variable, = or <- x=5 x x<-6 x y<-x^2 y x<- log(10)*exp(5) x #Matrix Algebra in R # Create a vector of consecutive integers x<--1:10 x # Create a vector of sequence from -1 to 1 with 0.1 increment x<-seq(0,1,0.01) x # Scalar addition (2 is added to each component) x+2 # Scalar multiplication (multiplication of 2 for each component) x*2 # summation, maximum and minimum of elements in x x<-1:10 sum(x) min(x) max(x) # sorting sort(x) # increasing order sort(x, decreasing=T) # decreasing order # Matrix Operations # number of elements in x x<-c(1,3,5,7,9,11) length(x) x[3] # third element x[2:5] # the third to fifth element of x x[-2] # all except the second element x[x>5] # list of elements in x greater than 5 # Matrix multiplication, cross product #Row vector x<-matrix(c(1,2,3),nrow=1) x #Column vector y<-matrix(c(1,2,1),nrow=3) y z<-matrix(c(2,3,4),nrow=3) z y+z # element by element addition y*z # element by element multiplication # Matrix Defition # 3 by 3 matrix # by column direction m1<-matrix(c(1,3,2,5,-1,2,2,3,9),nrow=3) m1 # by row direction m2<-matrix(c(1,3,2,5,-1,2,2,3,9),ncol=3,byrow=T) m2 #Cross Product m1%*%m2 #Check for dimension dim(m2) #Extracting the components of matrix m2 m2[2,3] # extracting an element of 2nd row-3rd column m2[2, ] # extracting 2nd row m2[ ,3] # extracting 3rd column m2[2:3,2:3] # extracting 2 and 3rd row and 2 adn 3rd column # Matrix operations m3<-matrix(c(2,3,4,5),nrow=2) m3 t(m3) # transpose solve(m3) # inverse matrix of m3, works for square matrix solve(m2) # Data input and summary statistics score<-c(31,45,48,55,57,58,67,68,73,75,78,80,82,85,88,89,91,92,95,99) mean(score) median(score) var(score) sd(score) IQR(score) #Covariance and Correlation x<-c(12, 30,15,24,14) y<-c(20,60,27,50,23) cov(x,y) cor(x,y) # Working Directory, setting..NOTE: make sure the seperator is / instead of \. setwd("C:/Shihomi/Shihomi_Office/Courses_HU/2014_2015_Guz/ECO239/R_workplace") # Loading libraries after installing each package library(AER) library(ECDAT) # Downloading data in .csv format housingdata <- read.table("C:/Shihomi/Shihomi_Office/Courses_HU/2015_2016_Guz/ECO239/RHW/HousingDataSRT.csv", header=TRUE, sep=",") ls(housingdata) # list the names of variables # Downloading data in .xlsx format library(xlsx) housingdata2 <-read.xlsx("C:/Shihomi/Shihomi_Office/Courses_HU/2015_2016_Guz/ECO239/RHW/HousingDataSRT.xlsx",1) ls(housingdata2) #Define each variable #ID COUNTY DPRICE LOTACR BATHN GRGSQF AGE AIRCNDD YR COUNTY<-housingdata[,2] DPRICE<-housingdata[,3] LOTACR<-housingdata[,4] BATHN<-housingdata[,5] GRGSQF<-housingdata[,6] AGE<-housingdata[,7] AIRCNDD<-housingdata[,8] YR<-housingdata[,9] ##NOTE: Names are case-sensitive. "COUNTY" is different from "county". #Summary Statistics mean(DPRICE) median(DPRICE) var(DPRICE) sd(DPRICE) IQR(DPRICE) cov(DPRICE,LOTACR) cor(DPRICE,LOTACR) cov(DPRICE,AGE) cor(DPRICE,AGE) #Plots # Simple Bar Plot counts <- table(housingdata$COUNTY) barplot(counts, main="COUNTY DISTRIBUTION", xlab="COUNTY") #Histogram # Simple Histogram hist(housingdata$DPRICE) # Colored Histogram with Different Number of Bins hist(housingdata$DPRICE, breaks=12, col="green") hist(housingdata$AGE, breaks=20, col="red") # Scatter Plot attach(housingdata) plot(AGE, DPRICE, main="Impact of AGE on Housing Price", xlab="AGE ", ylab="DPRICE ", pch=19) # 3D Scatterplot library(scatterplot3d) attach(housingdata) scatterplot3d(AGE,DPRICE,LOTACR, main="3D Scatterplot") # Download data in data packages #For example, use Housing data from ECDAT package data(Housing) ls(Housing) airco <-Housing[,1] bathrms<-Housing[,2] bedrooms<-Housing[,3] driveway<-Housing[,4] fullbase<-Housing[,5] garagepl<-Housing[,6] # Mean mean(bedrooms) # standard dev sd(bedrooms) # median median(bedrooms) # Inter quartile range (75. percentile - 25. percentile) IQR(bedrooms) # range range(bedrooms) # save workspace save.image("C:/Shihomi/Shihomi_Office/Courses_HU/2015_2016_Guz/ECO239/R_workplace/ECO239_Oct22_2015.RData")