#####Day2#####
# Statistics with R
# Qualitative Data
library(MASS) # load the MASS package
head(painters)
help(painters)
str(painters)
levels(painters$School)
school = painters$School
[Link] = table(painters$School)
class([Link])
cbind([Link])
[Link] = [Link] / nrow(painters)
[Link]
cbind([Link])
####BarPlot######
barplot([Link])
colors = c("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan")
barplot([Link], col = colors)
palette()
####Pichart###
pie([Link])
pie([Link], col = colors)
cSchool = school == 'C'
cPainters = painters[cSchool,]
mean(painters$Composition)
tapply(painters$Composition, painters$School, mean)
tapply(painters$Colour, painters$School, mean)
hist(painters$Colour)
# Quantitative
head(faithful)
help(faithful)
duration = faithful$eruptions
range(duration)
breaks = seq(1.5, 5.5, by=0.5)
[Link] = cut(duration, breaks, right=FALSE)
[Link] = table([Link])
cbind([Link])
####Histogram####
hist(duration)
hist(duration,
right=FALSE,
col=colors,
main="Old Faithful Eruptions",
xlab="Duration minutes")
[Link] = [Link] / nrow(faithful)
[Link] = cumsum([Link])
cumfreq0 = c(0, cumsum([Link]))
plot(breaks, cumfreq0,
main="Old Faithful Eruptions",
xlab="Duration minutes",
ylab="Cumulative eruptions")
lines(breaks, cumfreq0)
[Link] = cumsum([Link])
cumrelfreq0 = c(0, cumsum([Link]))
plot(breaks, cumrelfreq0,
main="Old Faithful Eruptions",
xlab="Duration minutes",
ylab="Cumulative releruptions")
lines(breaks, cumrelfreq0)
Fn = ecdf(duration)
plot(Fn,
main="Old Faithful Eruptions",
xlab="Duration minutes",
ylab="Cumulative eruption proportion")
stem(duration)
duration = faithful$eruptions
waiting = faithful$waiting
plot(duration, waiting,xlab="Eruption duration", ylab="Time waited", col = "red")
abline(lm(waiting ~ duration))
###Summary Statistics#####
mean(duration)
median(duration)
quantile(duration)
quantile(duration, c(.32, .57, .98))
range(duration)
max(duration)-min(duration)
####Inter Quartile Range######
IQR(duration)
summary(duration)
boxplot(duration, horizontal=FALSE)
var(duration)
sd(duration)
cov(duration, waiting)
cor(duration, waiting)
##############################
library(e1071)
moment(duration, order=4, center=TRUE)
skewness(duration)
kurtosis(duration)
dbinom(4, size=12, prob=0.2)
pbinom(4, size=12, prob=0.2)
ppois(16, lambda=12) # lower tail
ppois(16, lambda=12, lower=FALSE) # upper tail
###Generating random numbers
runif(1)
runif(4)
# Get a vector of 3 numbers from 0 to 100
runif(3, min=0, max=100)
# Get 3 integers from 0 to 100
# Use max=101 because it will never actually equal 101
floor(runif(3, min=0, max=101))
# This will do the same thing
sample(1:100, 3, replace=TRUE)
# To generate integers WITHOUT replacement:
sample(1:100, 3, replace=FALSE)
rnorm(4)
# Use a different mean and standard deviation
rnorm(4, mean=50, sd=10)
# To check that the distribution looks right, make a histogram of the numbers
x <- rnorm(400, mean=50, sd=10)
hist(x)
####repeatable sequences of random numbers
[Link](423)
runif(3)
[Link](423)
runif(3)
# Save the seed
oldseed <- .[Link]
runif(3)
# Restore the seed
.[Link] <- oldseed
# Get the same random numbers as before, after saving the seed
runif(3)
####Rounding numbers######
x <- seq(-2.5, 2.5, by=.5)
# Round to nearest, with .5 values rounded to even number.
round(x)
# Round up
ceiling(x)
# Round down
floor(x)
# Round toward zero
trunc(x)
x <- c(.001, .07, 1.2, 44.02, 738, 9927)
# Round to one decimal place
round(x, digits=1)
# Round to tens place
round(x, digits=-1)
# Round to nearest 5
round(x/5)*5
# Round to nearest .02
round(x/.02)*.02
#####Creating strings from variables####
a <- "apple"
b <- "banana"
# Put a and b together, with a space in between:
paste(a, b)
# With no space, use sep="", or use paste0():
paste(a, b, sep="")
paste0(a, b)
# With a comma and space:
paste(a, b, sep=", ")
# With a vector
d <- c("fig", "grapefruit", "honeydew")
# If the input is a vector, use collapse to put the elements together:
e <- paste(d, collapse=", ")
class(e)
# If the input is a scalar and a vector, it puts the scalar with each
# element of the vector, and returns a vector:
paste(a, d)
# Use sep and collapse:
paste(a, d, sep="-", collapse=", ")
a <- "string"
sprintf("This is where a %s goes.", a)
x <- 8
sprintf("Regular:%d", x)
# Can print to take some number of characters, leading with spaces.
sprintf("Leading spaces:%4d", x)
# Can also lead with zeros instead.
sprintf("Leading zeros:%04d", x)
sprintf("%f", pi) # "3.141593"
sprintf("%.3f", pi) # "3.142"
sprintf("%1.0f", pi) # "3"
sprintf("%5.1f", pi) # " 3.1"
sprintf("%05.1f", pi) # "003.1"
sprintf("%+f", pi) # "+3.141593"
sprintf("% f", pi) # " 3.141593"
sprintf("%-10f", pi) # "3.141593 " (left justified)
sprintf("%e", pi) #"3.141593e+00"
sprintf("%E", pi) # "3.141593E+00"
sprintf("%g", pi) # "3.14159"
sprintf("%g", 1e6 * pi) # "3.14159e+06" (exponential)
sprintf("%.9g", 1e6 * pi) # "3141592.65" ("fixed")
sprintf("%G", 1e-6 * pi) # "3.14159E-06"
x <- "string"
sprintf("Substitute in multiple strings: %s %s", x, "string2")
# To print a percent sign, use "%%"
sprintf("A single percent sign here %%")
#####Creating a formula from a string#######
# This returns a formula:
[Link]("y ~ x1 + x2")
# These are the variable names:
measurevar <- "y"
groupvars <- c("x1","x2","x3")
# This creates the appropriate string:
paste(measurevar, paste(groupvars, collapse=" + "), sep=" ~ ")
[Link](paste(measurevar, paste(groupvars, collapse=" + "), sep=" ~ "))
#####Extracting components from a formula######
f <- y ~ x1 + x2
# Take a look at f
str(f)
# Get each part
f[[1]]
f[[2]]
f[[3]]
# Or view the whole thing as a list
[Link](f)
f2 <- ~ x1 + x2
[Link](f2)
str(f[[1]])
str(f[[2]])
str(f[[3]]) ###language object (which consists of multiple symbols
# Look at parts of the langage object
str(f[[3]][[1]])
str(f[[3]][[2]])
str(f[[3]][[3]])
# The language object gets coerced into a string that represents the parse tree:
[Link](f[[1]])
[Link](f[[2]])
[Link](f[[3]])
# You can use deparse() to get a more natural looking string
deparse(f[[3]])
deparse(f)
environment(f)