Sie sind auf Seite 1von 10

> ###################################################

> ## Distribution Plots, QQ plot, and Normal Probability Plot


> ###################################################
> x <- sample(1:10, 30, replace=T)
> stripchart(x, method="stack", pch=19, cex=1) #Construct a dotplot

> boxplot(x) # Construct a boxplot


> y=runif(50,0,20)
> stem(y) # Construct a stemplot

The decimal point is at the |

0 | 014479089
2 | 993
4 | 2701122488
6|5
8 | 0451
10 | 05626
12 | 0236
14 | 5
16 | 188
18 | 04671269
20 | 00

> hist(y, breaks=10) # Construct a histogram


> qqplot(y,x) # Construct a QQ plot
> abline(0,1)
>
>
> x=rnorm(100,5, 2)
> qqnorm(x) # Construct a normal probability plot
> qqline(x)
> abline(mean(x),sd(x))
>
>
> ###################################################
> ## T Test
> ###################################################
> help(t.test) # T test, using t.test(x,y) where x and y are 2 sample values to
compare.
> # or using t.test(y~x) where y is a vector of data values for the two
samples
> # and x is the group indicator. See example below
>
> BurnTime=read.table("Ch2#21.txt", sep="\t", header=TRUE) # Problem 2.21
page 57t
> names(BurnTime)
[1] "Type.1" "Type.2"
> attach(BurnTime)
> boxplot(Type.1, Type.2, xlab="Formulation", ylab="Burning Time (in minutes)"",
names=c("Type 1","Type 2"))
>

> t.test(Type.1, Type.2) # This is the first way.

Welch Two Sample t-test

data: Type.1 and Type.2


t = 0.048, df = 17.998, p-value = 0.9622
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-8.552517 8.952517
sample estimates:
mean of x mean of y
70.4 70.2

>
> Time=c(Type.1,Type.2) # This is the second way (bodeling approach) by
generating two new vactor of data.
> type=rep(c(1,2), each=10)
> t.test(Time~type)

Welch Two Sample t-test

data: Time by type


t = 0.048, df = 17.998, p-value = 0.9622
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-8.552517 8.952517
sample estimates:
mean in group 1 mean in group 2
70.4 70.2

> var.test(Type.1, Type.2) # Test for the equality of two variances.

F test to compare two variances

data: Type.1 and Type.2


F = 0.9782, num df = 9, denom df = 9, p-value = 0.9744
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.2429752 3.9382952
sample estimates:
ratio of variances
0.9782168

>
>
> tapply(Time, type, mean) # Another useful command to obtain summary
statistics for each group.
1 2
70.4 70.2
>
>
>
>
################################################################
#
## Operating Characteristic curve
#
################################################################
#
# ONE-TAILED TEST
# #Assume populations are normal with known and equal variances
#
################################################################
#
> par(cex.main=0.8, cex.lab=0.7)
> par(mfrow=c(1,2))
> alpha=0.05
> d=seq(0,3, 0.1)
> n=2
> beta2.1=pnorm(qnorm(1-alpha)-sqrt(2*n)*d)
> beta2.1
[1] 9.500000e-01 9.257505e-01 8.934072e-01 8.519547e-01 8.009037e-01
[6] 7.404890e-01 6.717872e-01 5.967151e-01 5.178880e-01 4.383530e-01
[11] 3.612400e-01 2.893973e-01 2.250806e-01 1.697518e-01 1.240152e-01
[16] 8.768546e-02 5.995561e-02 3.961714e-02 2.528290e-02 1.557519e-02
[21] 9.257705e-03 5.307158e-03 2.933294e-03 1.562604e-03 8.020880e-04
[26] 3.966150e-04 1.888844e-04 8.662010e-05 3.824390e-05 1.625397e-05
[31] 6.648902e-06
> n=10
> beta10.1=pnorm(qnorm(1-alpha)-sqrt(2*n)*d)
> beta10.1
[1] 9.500000e-01 8.844714e-01 7.735010e-01 6.191362e-01 4.427499e-01
[6] 2.771884e-01 1.495354e-01 6.868698e-02 2.662702e-02 8.654705e-03
[11] 2.347246e-03 5.292533e-04 9.893929e-05 1.530211e-05 1.954747e-06
[16] 2.059759e-07 1.788430e-08 1.278457e-09 7.518908e-11 3.636020e-12
[21] 1.445073e-13 4.718100e-15 1.265049e-16 2.784716e-18 5.031246e-20
[26] 7.459238e-22 9.073039e-24 9.052657e-26 7.407958e-28 4.971208e-30
[31] 2.735360e-32
> plot(d,beta2.1,type="l", ylim=c(0,1),main="OC Curve for One-tailed Test of Two
Means
+ with 0.05 significance level", ylab="Probability of accepting the null
hypothesis")
> lines(d,beta10.1, col="blue")
> text(0.8, 0.75, labels="n = 2",cex=0.7)
> text(0.7,0.4, labels="n = 10",cex=0.7, col="blue")
>
> #############################
> # Two-TAILED TEST
> #############################
> n=2
> beta2.2=pnorm(qnorm(1-alpha/2)-sqrt(2*n)*d)+
+ pnorm(qnorm(alpha/2)-sqrt(2*n)*d)
> n=10
> beta10.2=pnorm(qnorm(1-alpha/2)-sqrt(2*n)*d)+
+ pnorm(qnorm(alpha/2)-sqrt(2*n)*d)
> plot(d,beta2.2,type="l", ylim=c(0,1), main="OC Curve for Two-tailed Test of Two
Means
+ with 0.05 significance level", ylab="Probability of accepting the null
hypothesis")
> lines(d,beta10.2, col="blue")
> text(1, 0.75, labels="n = 2",cex=0.7)
> text(0.7,0.4, labels="n = 10" , cex=0.7, col="blue")
>
> #################################################
> # Comparing one-tailed and two-tailed tests
> #################################################
> dev.off()
null device
1
>
> plot(d,beta10.1,type="l", xlim=c(0,2),main="OC Curve for Testing Two Means
+ with 0.05 significance level and n =10", ylab="Probability of accepting the null
hypothesis")
> lines(d,beta10.2, col="blue")
> text(0.2, 0.25, labels="One-tailed Test",)
> text(0.8,0.3, labels="Two-tailed Test", col="blue")
> abline(v=0.5,lty=3)

Das könnte Ihnen auch gefallen