You are on page 1of 2

STAT 491/STA 691: Statistics for Scientists I

R Command list
• Assign a vector of data to a name

x <- c(14966, 5148, 3921, 1628, 1083, 425)

• Plots for qualitative variables

deaths <- c(14966, 5148, 3921, 1628, 1083, 425)


cause <- c("acc", "hom", "suic", "canc", "heart", "cong")

## bar plot
barplot(deaths, names.arg = cause, main = " ", xlab=" ", ylab=" ", col=" ")

## pie chart
pie(deaths, labels = cause, main=" ")

• Plots for quantitative variables

rate <- c(11, 12, 14, 18, 22, 22, 23, 23, 23, 27, 28, 29, 30, 33, 34, 35, 35, 40)

## histogram
hist(rate, main = " ", xlab=" ", ylab=" ", col=" ")

## choose the breaks for the histogram


hist(rate, main = " ", xlab=" ", ylab=" ", col=" ", breaks=c(10, 15, 20, 25, 30, 35, 40))

##dotplot
stripchart(rate, method="stack", offset=0.5, at=0, pch=19, xlab="", ylab="")

## stem-and-leaf plot
stem(rate)

• Numerical summaries for quantitative variables

pine.needles <- c(7.2, 7.6, 8.5, 8.5, 8.7, 9.0, 9.0, 9.3, 9.4, 9.4, 10.2, 10.9, 11.3, 12.1, 12.8)
n <- 15

## sample mean
sum(pine.needles)/n
mean(pine.needles)

## median
median(pine.needles)

## 6 number summary
summary(pine.needles)

## sample variance
sum( (pine.needles - mean(pine.needles))^2 )/(n-1)
var(pine.needles)

## sample standard deviation


sqrt(var(pine.needles))
sd(pine.needles)

• Box plot: visual 5 number summary

boxplot(pine.needles, main = " ", xlab=" ", ylab=" ", col=" ")

• Scatter plot

perc.return <- c(74, 66, 81, 52, 73, 62, 52, 45, 62, 46, 60, 46, 38)
new.adults <- c(5, 6, 8, 11, 12, 15, 16, 17, 18, 18, 19, 20, 20)

## scatter plot
plot(perc.return, new.adults, main = " ", xlab=" ", ylab=" ", col=" ")

• Loading data from a file

1
empathy <- read.table("empathy.txt", header=TRUE)
empathy

• Attaching a data set to access the column names

attach(empathy)
sub
emp
brain

• Detaching a data set

detach(empathy)

• Randomly sample 10 numbers from 1 to 25 WITHOUT replacement:

sample(1:25, 10, replace = FALSE)

• Give a name to the sampled random values:

sam1 <- sample(1:25, 10, replace = FALSE)


sam1

• Randomly sample 10 numbers from 1 to 25 WITH replacement:

sample(1:25, 10, replace = TRUE)

• Determine the probability to the left of z = 1 using the standard normal distribution P (Z < 1) =?:

pnorm(1)

• Determine the z ∗ such that to the left of z ∗ there is a probability of 0.84 using the standard normal distribution P (Z < z ∗ ) = 0.84:

qnorm(0.84)

• Generate a random sample of size n = 10 from a normal distribution with µ = 5 and σ = 2:

y <- rnorm(100, mean=5, sd=2)


y
hist(y)
mean(y)
sd(y)

• Normal quantile-quantile plot for y:

qqnorm(y)

• Take the square-root of a number:

sqrt(36)

• The t-distribution:

– Determine the probability to the left of t = 1 with degrees of freedom equal to 5 using the t-distribution P (tdf =f < 1) =?:
pt(1, df=5)
– Determine the t∗ such that to the left of t∗ is a probability of 0.82 using the t-distribution with 5 degrees of freedom
P (tdf =f < t∗ ) = 0.82:
qt(0.82, df=5)

• One sample t-test:

x <- c(29, 27, 34, 40, 22, 28, 14, 35, 26, 35, 12, 30, 23, 18, 11, 22, 23, 33)
t.test(x, alternative = "two.sided", mu=0, conf.level = 0.95)
t.test(x, alternative = "less", mu=0, conf.level = 0.95)
t.test(x, alternative = "greater", mu=0, conf.level = 0.95)

• Two sample t-test. Testing H0 : µx − µy = 0 with σx and σy assumed not to be equal.

x <- c(1311, 1250, 1292, 1419, 1401, 1297, 1202, 1336, 1308, 1353, 1515, 1461, 1365)
y <- c(1040, 1180, 1207, 1179, 1115, 1133, 1298, 1263, 1194, 1198, 1230, 1114)
t.test(x, y, alternative="two.sided", conf.level = 0.95, mu=0, var.equal = FALSE)
t.test(x, y, alternative="less", conf.level = 0.95, mu=0, var.equal = FALSE)
t.test(x, y, alternative="greater", conf.level = 0.95, mu=0, var.equal = FALSE)

You might also like