knitr::opts_chunk$set(echo = TRUE)

Task 1

getwd()

Task 2

Create layout for 4 plots, plot the given curves

layout.matrix <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2, byrow = T)

layout(mat = layout.matrix,
       heights = c(2, 2), # Heights of the two rows
       widths = c(2, 2)) # Widths of the two columns

curve(dnorm(x,mean=10,sd = 4), xlim = c(10-(3*4), 10+(3*4)))

curve(dnorm(x,mean=10,sd = 2), xlim = c(10-(3*2), 10+(3*2)))
curve(dnorm(x,mean=5,sd = 10), xlim = c(5-(3*10), 5+(3*10)))
curve(dnorm(x,mean=5,sd = .5), xlim = c(5-(3*.5), 5+(3*.5)))

Y∼N(0,1),P(Y≥2)

mean = 0
sd = 1
a = 2
b = mean+(3*sd)
curve(dnorm(x,mean=mean,sd = sd), xlim = c(mean-(3*sd), mean+(3*sd)))
xcurve = seq(a,b, length=1000)
ycurve = dnorm(xcurve, 0, 1)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Blue")
# Area -- We want the area from [2, +infinity], but we know that for this graph, y approaches 0
# as we move to positive infinity. The value stops changing at b=6, anything greater remains the same.
prob=pnorm(b+3,mean=mean,sd=sd)-pnorm(a,mean=mean,sd=sd)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Y∼N(μ=4,σ=2),P(1≤Y<5)

mean = 4
sd = 2
a = 1
b = 4.99999 #we want close to 5 as we can get, but not 5 (value does not change closer than 4.99999)
curve(dnorm(x,mean=mean,sd = sd), xlim = c(mean-(3*sd), mean+(3*sd)))
xcurve = seq(a,b, length=1000)
ycurve = dnorm(xcurve, mean, sd)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Grey")

prob=pnorm(b,mean=mean,sd=sd)-pnorm(a,mean=mean,sd=sd)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Y∼N(μ=10,σ=4),P(Y<10)

mean = 10 
sd = 4
a = -6
b = 10 
curve(dnorm(x,mean=mean,sd = sd), xlim = c(mean-(3*sd), mean+(3*sd)))
xcurve = seq(a,b, length=1000)
ycurve = dnorm(xcurve, mean, sd)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Pink")
# p(y < 10) + p(y >= 10) = 1
prob= 1 - pnorm(b,mean,sd )
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Y∼N(μ=-2,σ=1/2),P(-3 < Y ≤ -2)

mean = -2 
sd = .5
a = -3
b = -2 
curve(dnorm(x,mean=mean,sd = sd), xlim = c(mean-(3*sd), mean+(3*sd)))
xcurve = seq(a,b, length=1000)
ycurve = dnorm(xcurve, mean, sd)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Brown")

prob=pnorm(b,mean=mean,sd=sd)-pnorm(a,mean=mean,sd=sd)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Task 3

Plot the curves on one graph

curve(dgamma(x,shape=1,scale=1),xlim=c(0,10),ylim=c(0,1),col="Red",lwd=2,
ylab="Gamma density", main="Task 3")
curve(dgamma(x,shape=3,scale=1),xlim=c(0,10),ylim=c(0,1),col="Blue",lwd=2,
ylab="Gamma density", add=T)
curve(dgamma(x,shape=5,scale=1),xlim=c(0,10),ylim=c(0,1),col="Green",lwd=2,
ylab="Gamma density", add=T)

Y~Gamma(shape=3,scale=2),P(2<Y<5)

curve(dgamma(x,shape=3,scale=2),xlim=c(0,10),ylim=c(0,1),col="Red",lwd=2,
ylab="Gamma density", main="    Y~Gamma(shape=3,scale=2),P(2<Y<5)")

a = 2
b = 5 

xcurve = seq(a,b, length=10000)
ycurve = dgamma(xcurve, shape = 3, scale = 2)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Yellow")

prob=pgamma(b,shape = 3,scale = 2) - pgamma(a,shape = 3,scale = 2)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Y∼Gamma(shape=6,scale=3),P(1≤Y≤4)

curve(dgamma(x,shape=6,scale=3),xlim=c(0,10),ylim=c(0,.05),col="Blue",lwd=2,
ylab="Gamma density", main="Y∼Gamma(shape=6,scale=3),P(1 <= Y <= 4)")

a = 1
b = 4 

xcurve = seq(a,b, length=1000)
ycurve = dgamma(xcurve, shape = 6, scale = 3)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Yellow")

prob=pgamma(b,shape=6,scale=3)
prob=round(prob,4)
text((a+b)/2, .05*.1 ,paste0("Area = ", prob))

Y∼Gamma(shape=2,scale=4),P(3 ≤ Y < 6)

curve(dgamma(x,shape=2,scale=4),xlim=c(0,10),ylim=c(0,1),col="Black",lwd=2,
ylab="Gamma density", main="Y∼Gamma(shape=2,scale=4),P(1 <= Y <= 4)")

a = 3
b = 6 

xcurve = seq(a,b, length=1000)
ycurve = dgamma(xcurve, shape = 2, scale = 4)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Red")

prob=pgamma(b,shape=2,scale=4) - pgamma(a, shape=2,scale=4)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Task 4

Use layout to plot 4 curves

layout.matrix <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2, byrow = T)

layout(mat = layout.matrix,
       heights = c(2, 2), # Heights of the two rows
       widths = c(2, 2)) # Widths of the two columns

#   Y∼chisq(df=1)
curve(dchisq(x,df=1),xlim=c(0,10),ylim=c(0,1),col="Red",lwd=1,
ylab="Chisq density", main="df=1")
# Y∼chisq(df=2)
curve(dchisq(x,df=2),xlim=c(0,10),ylim=c(0,1),col="Blue",lwd=2,
ylab="Chisq density", main="df=2")
# Y∼chisq(df=4)
curve(dchisq(x,df=4),xlim=c(0,10),ylim=c(0,1),col="Green",lwd=3,
ylab="Chisq density", main="df=4")
# Y∼chisq(df=20)
curve(dchisq(x,df=20),xlim=c(0,10),ylim=c(0,1),col="Black",lwd=4,
ylab="Chisq density", main="df=20")

Y∼chisq(df=2),P(2≤Y≤4)

curve(dchisq(x,df=2),xlim=c(0,10),ylim=c(0,1),col="Black",lwd=2,
ylab="Chisq Density", main="Y∼chisq(df=2),P(2≤Y≤4)")

# Chisq is continuous so boundaries aren't critical
a = 2
b = 4 

xcurve = seq(a,b, length=1000)
ycurve = dchisq(xcurve,df=2)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Red")

prob=pchisq(b,df=2) - pchisq(a, df=2)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Y∼chisq(df=3),P(3≤Y≤5)

curve(dchisq(x,df=3),xlim=c(0,10),ylim=c(0,1),col="Black",lwd=2,
ylab="Chisq Density", main="Y∼chisq(df=3),P(3≤Y≤5)")

# Chisq is continuous so boundaries aren't critical
a = 3
b = 5

xcurve = seq(a,b, length=1000)
ycurve = dchisq(xcurve,df=3)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Red")

prob=pchisq(b,df=3) - pchisq(a, df=3)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Y∼chisq(df=20),P(10<Y≤21)

curve(dchisq(x,df=20),xlim=c(4,25),ylim=c(0,.3),col="Black",lwd=2,
ylab="Chisq Density", main="Y∼chisq(df=20),P(10<Y≤21) ")

# Chisq is continuous so boundaries aren't critical
a = 10
b = 21

xcurve = seq(a,b, length=1000)
ycurve = dchisq(xcurve,df=20)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Red")

prob=pchisq(b,df=20) - pchisq(a, df=20)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Task 5

Use layout to plot 4 curves

layout.matrix <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2, byrow = T)

layout(mat = layout.matrix,
       heights = c(2, 2), # Heights of the two rows
       widths = c(2, 2)) # Widths of the two columns
# Shape = scale = 1
curve(dweibull(x,shape = 1, scale = 1),xlim=c(0,10),ylim=c(0,1),col="Red",lwd=1,
ylab="Weibull Density", main="shape = scale = 1")
# Shape = 2, scale = 4
curve(dweibull(x,shape = 2, scale = 4),xlim=c(0,10),ylim=c(0,1),col="Blue",lwd=1,
ylab="Weibull Density", main="Shape = 2, Scale = 4")
# Shape = 7, scale = 7
curve(dweibull(x,shape = 7, scale = 7),xlim=c(0,10),ylim=c(0,1),col="Black",lwd=1,
ylab="Weibull Density", main="Shape = 7, Scale = 7")
# Shape = 3, scale = 7
curve(dweibull(x,shape = 3, scale = 7),xlim=c(0,10),ylim=c(0,1),col="Green",lwd=1,
ylab="Weibull Density", main="Shape = 3, Scale = 7")

Y∼weibull(shape=1,scale=1),P(y >= 3)

curve(dweibull(x,shape=1, scale=1),xlim=c(0,10),ylim=c(0,1),col="Black",lwd=2,
ylab="Weibull Density", main="Y∼weibull(shape=1,scale=1),P(y >= 3)")

a = 3

xcurve = seq(a, length=1000)
ycurve = dweibull(xcurve,shape=1,scale=1)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Red")

prob= pweibull(a, shape=1, scale=1)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Y∼weibull(shape=2,scale=4),P(y >= 3)

curve(dweibull(x,shape=2, scale=4),xlim=c(0,10),ylim=c(0,1),col="Black",lwd=2,
ylab="Weibull Density", main="Y∼weibull(shape=2,scale=4),P(y >= 3)")

a = 3

xcurve = seq(a, length=1000)
ycurve = dweibull(xcurve,shape=2,scale=4)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Red")

prob= pweibull(a, shape=2, scale=4)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Y∼weibull(shape=7,scale=7),P(5 <= y <=6)

curve(dweibull(x,shape=7, scale=7),xlim=c(0,10),ylim=c(0,1),col="Black",lwd=2,
ylab="Weibull Density", main="Y∼weibull(shape=7,scale=7),P(y >= 5)")

a = 5
b = 6
xcurve = seq(a,b, length=1000)
ycurve = dweibull(xcurve,shape=7,scale=7)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Red")

prob= pweibull(b, shape=7, scale=7) - pweibull(a, shape=7, scale=7)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Task 6

Use layout to plot 4 curves

layout.matrix <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2, byrow = T)

layout(mat = layout.matrix,
       heights = c(2, 2), # Heights of the two rows
       widths = c(2, 2)) # Widths of the two columns
# Shape1 = 2, shape2=3
curve(dbeta(x,shape1 = 2, shape2 = 3),xlim=c(0,1),col="Red",lwd=1,
ylab="Beta Density", main="shape1 = 2 shape2 = 3")
# Shape1 = 1, shape2=3
curve(dbeta(x,shape1 = 1, shape2 = 3),xlim=c(0,1),col="Red",lwd=1,
ylab="Beta Density", main="shape1 = 1 shape2 = 3")
# Shape1 = 6, shape2=1
curve(dbeta(x,shape1 = 6, shape2 = 1),xlim=c(0,1),col="Red",lwd=1,
ylab="Beta Density", main="shape1 = 6 shape2 = 1")
# Shape1 = 2, shape2=2
curve(dbeta(x,shape1 = 2, shape2 =2),xlim=c(0,1),col="Red",lwd=1,
ylab="Beta Density", main="shape1 = 2 shape2 = 2")

Y~beta(shape1=2, shape2=2)(0.0 <= Y <= 0.2)

curve(dbeta(x,shape1=2, shape2 = 2),xlim=c(0,1),col="Black",lwd=2,
ylab="Beta Density", main="Y~beta(shape1=2, shape2=2)(0.0 <= Y <= 0.2)")

a = 0
b = .2
xcurve = seq(a,b, length=1000)
ycurve = dbeta(xcurve,shape1=2,shape2=2)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Red")

prob= pbeta(b, shape1=2, shape2=2) - pbeta(a, shape1=2, shape2=2)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Y~beta(shape1=2, shape2=3)(0.8 <= Y <= 0.9)

curve(dbeta(x,shape1=2, shape2 = 3),xlim=c(0,1),col="Black",lwd=2,
ylab="Beta Density", main="Y~beta(shape1=2, shape2=3)(0.8 <= Y <= 0.9)")

a = .8
b = .9
xcurve = seq(a,b, length=1000)
ycurve = dbeta(xcurve,shape1=2,shape2=3)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Red")

prob= pbeta(b, shape1=2, shape2=3) - pbeta(a, shape1=2, shape2=3)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Y~beta(shape1=1, shape2=3)(0.2 <= Y <= 0.4)

curve(dbeta(x,shape1=1, shape2 = 3),xlim=c(0,1),col="Black",lwd=2,
ylab="Beta Density", main="Y~beta(shape1=1, shape2=3)(0.2 <= Y <= 0.4)")

a = .2
b = .4
xcurve = seq(a,b, length=1000)
ycurve = dbeta(xcurve,shape1=1,shape2=3)
polygon(c(a,xcurve,b),c(0,ycurve,0),col="Red")

prob= pbeta(b, shape1=1, shape2=3) - pbeta(a, shape1=1, shape2=3)
prob=round(prob,4)
text((a+b)/2, .5*.1 ,paste0("Area = ", prob))

Task 7

grac0009MATH4753::myncurve(mu=10,sigma=5, a=6)


agracy2246/MATH4753grac0009 documentation built on April 26, 2020, 9:39 a.m.