Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

#Exercise 1:

getwd()
setwd("C:/bes/lab 2")
data1 <- read.table("mtcars.csv",header=TRUE,sep=",",quote="\"",
stringsAsFactors = FALSE)
data1$am <- factor(data1$am,ordered=FALSE,levels=c(0,1),labels =
c("automatic","manual"))
data2 <- read.table("mtcars.csv",header=TRUE, sep=",",quote="\"",
stringsAsFactors=TRUE)
am.table <- table(data1$am)
prop.table(am.table)
prop.table(am.table)*100

#Exercise 2:
data1$gear <- factor(data1$gear,ordered=TRUE,levels=c(3,4,5),labels =
c("3gears","4gears","5gears"))
gearVSam.table2 <- table(data1$am,data1$gear)
gearVSam.table2

#Simple bar graph


am.table <- table(data1$am)
barplot(am.table)
barplot(am.table, main="Bar graph of Transmission", xlab="Types of
Transmission", ylab="Frequency",ylim=c(0,20))
barplot(am.table, main="Bar graph of Transmission", xlab="Types of
Transmission", ylab="Frequency", horiz=TRUE)
barplot(am.table, col="skyblue",main="Bar graph of Transmission ",
xlab=" Types of Transmission ", ylab="Frequency")
barplot(am.table, col=c("blue","pink"),beside=TRUE,ylim=c(0,20))
#EX3:Clustered bar graph with two variables
#3c
data1 <- read.table("mtcars.csv",header=TRUE,sep=",",quote="\"",
stringsAsFactors = FALSE)
data1$gear <- factor(data1$gear,ordered=FALSE,levels=c(3,4,5),labels =
c("3gears","4gears","5gears"))
#3a
trans.vs.gear<-table(data1$am,data1$gear)
trans.vs.gear
barplot(trans.vs.gear, beside=TRUE)#
barplot(trans.vs.gear, col=c("red", "yellow"),main="Bar graph of Transmisson
and Gear",xlab="Number of Gear",ylab="Types of Transmission",
beside=TRUE,ylim=c(0,20))
#3b
barplot(trans.vs.gear, col=c("green", "pink"),main="Bar graph of Transmisson
and Gear",xlab="Number of Gear",ylab="Types of Transmission",
beside=TRUE,ylim=c(0,20))
barplot(trans.vs.gear, col="skyblue",main="Bar graph of Transmission ",
xlab=" Types of Transmission ", ylab="Frequency")

#Stacked bar graph


barplot(trans.vs.gear,col=c("red","yellow"))
spineplot(trans.vs.gear, col=c("blue", "green", "pink"))

#Stem and leaf display


mpg <- data1$mpg
mpg <- data1$am
stem(mpg)
#Histogram
hist(mpg)
hist(mpg,breaks=5,col="red")
hist(mpg, freq=FALSE, breaks=5,col="red", ylim=c(0,0.08))

#Boxplot
boxplot(data1$mpg)
boxplot.stats(data1$mpg)
boxplot(data1$mpg ~ data1$gear)

title(main="Boxplots between Number of gears vs Transmission", sub="my sub-


title",
xlab="Number of gears", ylab="yaxis label")

#Paired samples t-test


data3 <- read.table("GOlfScores.csv",header=TRUE,sep=",",quote="\"",
stringsAsFactors = FALSE)
#t.test (y1, y2, paired=TRUE, alternative = .,conf.level=0.95)
t.test(data3$First, data3$Final,paired=TRUE,
alternative="two.sided",conf.level=0.90)

Difference <- data3$First - data3$Final


stem(Difference)
qqnorm(Difference,main="Normal q-q plot of golf score differences")
qqline(Difference,col=2,lwd=2,lty=2)

#Excercise5
Data4 <- read.table("PriceChange",header=TRUE,sep=",",quote="\"",
stringsAsFactors = FALSE)
Extracted5<- data1[1:3,]
Summary(data4[c(“Jan”, “Apr”)])
t.test (data4$Jan, data4$Apr, paired=TRUE, alternative=”two.sided”,
conf.level=0.90)
Reject Ho if p-value = 0.06 < alpha = 0.1 Reject Ho and we have enough evidence that
the recession was affecting the stock market.

Difference <- data4$Jan – data4$Apr


stem(Difference)
qqnorm(Difference,main="Normal q-q plot of price change")
qqline(Difference,col=2,lwd=2,lty=2)

You might also like