..
R8
# Parameters for ~N are mean and std deviation
#Parameters for ~Binom are n and p= Pr(success in each trial)
#COMMANDS when working with brand-name distrubutions in R
# pdistibution_name(x_value,parameters)= Pr(below); #ddist(x_val,paramsd_dist(x_val,params))
#ddist(x_val,params)= density for continuous r.v. OR Pr(X=x) for discrete
#q_dist(0.63, params.)= get the value of x which is that %tile
pnorm(56,79,10) #want Pr(x<56yrs, mean=79yrs, sd=10yrs) 0.010724
z=(56-79)/10; Z #-2.3
#If you want the z-score, use R as a calculator, as above.
pnorm(-2.3) # IF you have the z-score, can use pnorm without miu or sigma
#0.01072411 the same output; R assumes that -2.3=z, so ~N(0,1)
qnorm(0.12,79,1)# want x the 12th%tile of ~N(79,10) 67.25013
dnorm(56,79,10)# want the density ~N(79,10)above x=56 yrs 0.002832704
#suppose we want Pr(x>82,79,10), how likely is someone to live over 82 yrs
Pr_above_82=1-pnorm(82,79,10); Pr_above_82 # 0.3820886
# want Pr(80<x<90);
Plong=pnorm(90,79,10)-pnorm(80,79,10); Plong #[1] 0.3245061
# FOR BINOMIAL x is discete numeric
#If we want Pr(x=8 successes, 10 trials, p=0.578)
dbinom(8,10,0.578) # For discrete, ddist=Pr(X=x) 0.09982978
#Pr(exactly 8 successes in 1o trial if Pr(success in ea. trial)=.578
pbinom(8,10,0.578) #cumulative Pr(x< or = 8) #0.965453
#pbinom(x,n,p) =Pr(0)+Pr(1)+...+Pr(x)
#Pr between 3 and 7 successes, including 7: Pr(3<x<or =7)
pbinom(7,10,0.578)-pbinom(3,10,.578) #0.7926432 OR next line
dbinom(4,10,0.578)+dbinom(5,10,.578)+dbinom(6,10,.578)+dbinom(7,10,.578)
# 0.7926432 The same!
#R8 end
#############################################################
#R9 start : R9 is about using R as a calculator. Formulas below
# 95%CI for miu LOW=xbar-1.96*s/sqrt(n) HIGH=xbar+1.96*s/sqrt(n)
# OR: LOW=xbar+qnorm(.025*s/sqrt(n) HIGH=xbar+qnorm(.975)*s/sqrt(n)
# 2nd more accurate, but remember + qnorm, both: norm(.025)=neg
#99%CI for miu LOW=xbar+2.58*s/sqrt(n) HIGH=xbar+2.58*s/sqrt(n)
# OR LOW=xbar+qnorm(0.005)*s/sqrt(n) HIGH=xbar+qnorm(.995)*s/sqrt(n)
## 95%CI for Pi LOW=phat-1.96*sqrt(phat*(1-phat)/n)
## HIGH=phat+1.96*sqrt(phat*(1-phat)/n)
## 99% CI for Pi LOW=phat-2.58*sqrt(phat*(1-phat)/n)
## HIGH=phat+2.58*sqrt(phat*(1-phat)/n)
##OR 95%CI for Pi +qnorm(.025) & +qnorm(.975),instead of -+1.96
##and 99%CI for Pi +qnorm(.005)& +qnorm(.995), instead of -+2.58
#Want 95% CI for miu,given xbar=809.5,s=27.4,n=68; 1st way: 1.96
LOW95=809.5-1.96*24.4/sqrt(68); HIGH95=809.5+1.96*24.4/sqrt(68)
LOW95;HIGH95 #803.7005, 815.2995
CI95=c(LOW95,HIGH95)#Alternatively, can put Low and High into CI
CI95 # Get the printout all at once 803.7005 815.2995
#For 99%CI, the other way, +qnorm(.005) and +qnorm(.995)
LOW99_correct=809.5+qnorm(.005)*24.4/sqrt(68)
HIGH99_correct=809.5+qnorm(.995)*24.4/sqrt(68)
CI99=c(LOW99_correct, HIGH99_correct); CI99 #801.8783 817.1217
# Got phat=0.23 of those planning to vote for Bernie Sanders
# Want 95% CI for proportion in the pop. who willvote for Bernie
CI95_Pi_Bernie=c(0.23-1.96*sqrt(0.23*(1-0.23)/503),
0.23+1.96*sqrt(0.23*(1-0.23)/503))
CI95_Pi_Bernie #0.1932226 0.2667774 calculated -+1.96 way
CI95_Pi_Ber_more_exact=c(.23+qnorm(.025)*sqrt(.23*(1-.23)/503),
.23+qnorm(.975)*sqrt(.23*(1-.23)/503))
CI95_Pi_Ber #calculated more exact qnorm .025 and .975 way
# 0.1932232 0.2667768 Close! In any case, between 19 and 27%
# Are proportions ~N? Need to calculate n*phat and n*(1-phat)
503*.23 #115.69 >=10!
503*(1-.23) #387.31 >=10!
#Yes, phats ~N: 116 successes and 387 failures, way more than 5