Next, we will do a simple plot using the dummy data set "mtcars", available in the R
environment, to create a basic scatterplot. Let's use the columns "wt" and "mpg" in mtcars (wt
being weight, and mpg being miles per gallon). Ensure you understand what this line of code
is actually doing – if not, ask your tutor.
> input <- mtcars[c('wt','mpg')]
> plot(x = input$wt,y = input$mpg)
Construct a plot of wt against mpg for cars with weight between 2.5 to 5 and mileage between
15 and 30 and label it appropriately. The title of the plot should be: Weight vs Mileage”.
> plot(x = input$wt,y = input$mpg, xlab="Weight", ylab="Mileage", main="We
ight vs Mileage", xlim=c(2.5,5),
ylim=c(15,30))
http://www1.appstate.edu/
~arnholta/PASWR/CD/data/
Bodyfat
> site<-"http://www1.appstate.ed
u/~arnholta/PASWR/CD/data/Bo
dyfat"
> FAT<-read.table(file=site, header=TRUE, sep="\t")
> head(FAT)
age fat sex
1 23 9.5 M
2 23 27.9 F
3 27 7.8 M
4 27 17.8 M
5 39 31.4 F
6 41 25.9 F
> write.table(FAT,file="FAT.txt")
write.table(FAT,file="FAT.txt", sep="\t")
> mean(taxis)
> taxis<-
c(34400,45500,36700,32000,4
8400,32800,38100,30100)
> mean(taxis)
[1] 37250
> min(taxis)
[1] 30100
> max(taxis)
[1] 48400
> median(taxis)
Lab 2
[1] 35550
> var(taxis)
[1] 42860000
> sd(taxis)
[1] 6546.755
> summary(taxis)
Min. 1st Qu. Median Mean 3rd Qu. Max.
30100 32600 35550 37250 39950 48400
> fivenum(taxis)
[1] 30100 32400 35550 41800 48400
Enter the following data into R and call it mydata: 54, 59, 35, 41, 46, 25, 47, 60, 54, 46, 49,
46, 41, 34, 22. Now type
> stem(mydata)
> mydata<-c(54, 59, 35, 41, 46, 25, 47, 60, 54, 46, 49, 46, 41, 34, 22)
> stem(mydata)
The decimal point is 1 digit(s) to the right of the |
2 | 25
3 | 45
4 | 1166679
5 | 449
6 | 0
> boxplot(Cars93$Min.Price)
boxplot(Cars93$Min.Price, col="orange", horizontal =TRUE)
> boxplot(Cars93$MPG.city~Cars
93$Type)
> concrete<-
read.csv(file.choose())
> dim(concrete)
>fivenum(concrete$Concrete_c
ompressive_strength)
boxplot(concrete$Concrete_co
mpressive_strength, main="Compressive Strength
of Concrete", ylab="MPa",col="yellow")
1. Simulate tossing a coin 1000 times. Are the results what you would expect? > rbinom(1,1000,0.5) [1] 503
2. Suppose that n1 items are to be inspected from one production line and n2 items are to be inspected from another production line. Let p1 = the probability of a defective from line 1 and p2 = the probability of a defective from line 2. Let X be a binomial random variable with parameters n1 and p1. Let Y be a binomial random variable with parameters n2 and p2. A variable of interest is W
, which is the total number of defective items observed in both production lines. Let W = X + Y
. Use simulation to see how the distribution of W will behave. Useful information could be obtained by looking at the histogram of W
i’s generated and also considering the sample mean and the sample variance. In your simulation use the following random variables X and Y
: X is binomial with n1=7, p1=0.2; and Y is binomial with n2=8, p2=0.6. X <- rbinom(1000,7,0.2) Y <- rbinom(1000,8,0.6) mean(X) var(X) mean(Y) var(Y) mean(X+Y) var(X+Y) hist(X,freq = FALSE) hist(Y,freq = FALSE) hist(X+Y,freq = FALSE) The output is as follows: > mean(X) [1] 1.425 > var(X) [1] 1.129505 > mean(Y) [1] 4.862 > var(Y) [1] 1.952909 > mean(X+Y) [1] 6.287 > var(X+Y) [1] 3.111743
If X is a Normally distributed random variable with 𝜇=20 and 𝜎=5
. Calculate the following using R
: (i) 𝑃
(
𝑋
<15) (ii) 𝑃
(14<
𝑋
<23) (iii) Find k such that 𝑃(𝑋<𝑘)=0.9345
.