








Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!









o USING R
To find the sample mean and sample variance of data values. Suppose you want
the sample mean and sample variance of the values x 1
,... , x n
. Type
x = c(x 1
,... , x n
and hit return. [Interpret x = c(x 1
,... , x n
) as meaning that x is the sequence of
values x 1
,... , x n
.] On the next line type
mean(x )
and when you hit return, the sample mean is displayed. On the next line, type
var(x)
and when you hit return, the variance is displayed.
For instance, suppose you wanted the sample mean and sample variance of the
values 4, 6, 12, 9, 21, 14. Then
x = c( 4 , 6 , 12 , 9 , 21 , 14 )
mean(x)
var(x)
By the way, you do not type > at the beginning of a line. R always starts a line
with this symbol, which is a prompt asking what to do next. (The [1] in front of
the answers returned is to indicate that you only asked for a single item.)
Other commands are sum(x), which returns the sum of the values; median(x),
which returns the sample median; and sd(x), which returns the sample stan-
dard deviation (equal to the square root of var(x)).
If you have data pairs (x 1
, y 1
),... , (x n
, y n
), then to obtain the sample correlation
coefficient, do the following
x = c(x 1
,... , x n
y = c(y 1
,... , y n
cor(x,y)
To obtain a scatter plot of the data, type
plot(x,y)
and hit return.
For instance, suppose we have the data pairs (4, 10), (6, 13), (12, 22), (9, 15),
(21, 30), (14, 15), then we obtain the sample correlation coefficient and scatter
diagram as follows:
x = c( 4 , 6 , 12 , 9 , 21 , 14 )
y = c( 10 , 13 , 22 , 15 , 30 , 15 )
cor(x,y)
plot(x,y)
R can also be used for computation. For instance, suppose we wanted to
compute
!" • √
!&&
'&&
This is accomplished thusly:
Let Z be a standard normal. Then pnorm(x) returns P (Z ≤ x)
More generally, if X is normal with mean μ and variance σ
, then pnorm(x,μ,σ)
returns P(X≤x)
Of course, we could also have obtained P (X ≤ x) by
y=(x−μ)/σ > pnorm(y )
For instance, if X is normal with mean 10 and variance 9 , then P (X ≤ 12 ) can be
obtained either by
pnorm( 12 , 10 , 3 ) [ 1 ] 0. 7475075
or by
y=( 12 − 10 )/ 3
pnorm(y )
or by
pnorm(( 12 − 10 )/ 3 )
To determine the value z α
such that P (Z > z α
) = α
when Z is a standard normal random variable, just type qnorm( 1 − α). That is,
qnorm( 1 −α) returns z α
For instance, z
. 01
is obtained as follows:
qnorm( 1 −. 01 ) [ 1 ] 2. 326348
The mechanics of using a computer to choose a random sample are explained in
App. C. Program A-1 on the enclosed disk can be used to accomplish this task. R
can also be used. To use R to obtain a random sample of size n from a subset of
size N, just input
sample (N,n)
and the output will be the sample.
To test the null hypothesis that a mean μ is equal to μ 0
against the alternative
thatμ̸ =μ 0
when the data are x 1
,...,x n
,type
x=c(x 1
,...,x n
t.test(x, mu = μ 0
and hit return. Upon doing so, the output will include both a 95 percent
confidence interval and the p-value. To test that the mean is 9 when the data are
5, 8, 11, 14, 9, 11, 21, 16, do the following:
x = c(5,8,11,14,9,11,21,16)
t.test(x, mu = 9)
After hitting return after the second line, you will receive the following
output:
One Sample t-test
data: x
t = 1.6177, df = 7, p-value = 0.1498 alternative hypothesis: true mean is
not equal to 9 95 percent confidence interval: 7.672562 16.
sample estimates:
mean of x
The p value is 0.149 indicating that the null hypothesis would not be rejected at
any reasonable level of significance. Suppose you wanted a one-sided t-test;
say you wanted to test that μ ≤ 10 against the alternative hypothesis that μ > 10.
Then, you would type
x=c(x1,...,xn)
t.test(x , alternative = “greater”, mu = 10)
and hit return. Similarly, to test that μ ≥ 10 against the alternative μ < 10 you
would type
x=c(x1,...,xn)
t.test(x , alternative = “less”, mu = 10)
and hit return. With Inf meaning ∞, here is the output when testing H0 : μ ≤ 10
against the alternative H1 : μ > 10, when using the previously defined data x:
One Sample t-test
data: x
t = 1.055, df = 7, p-value = 0.1632 alternative hypothesis: true mean is greater
than 10 95 percent confidence interval: 8.507932 Inf sample estimates:
mean of x 11.875.
For instance, the test of Example 10.6 gives the following
x = c( 6. 5 , 6 , 8. 5 , 7 , 6. 5 , 8 , 7. 5 , 6. 5 , 7. 5 , 6 , 8. 5 , 7 )
y = c( 5. 5 , 6 , 7 , 6 , 7. 5 , 6 , 7. 5 , 5. 5 , 7 , 6. 5 )
t.test(x , y , alternative = “greater”, var.equal = TRUE)
Two Sample t-test
data: x and y
t = 1.8987, df = 20, p-value = 0.
alternative hypothesis: true difference in means is greater than 0 95
percent confidence interval: 0.06185013 Inf
sample estimates:
mean of x mean of y
Note in this case, that aside from outputting the p-value the result also gives a
one-sided 95 percent confidence interval for μ x
− μ y
To test H 0
: μ x
≥ μ y
against the alternative H 1
: μ x
< μ y
, use
x=c(x 1
,...,x n
y=c(y 1
,...,y m
t.test(x, y, alternative = “less”, var.equal = TRUE)
and hit return.
o Simple linear regression
estimated regression line using data (x1, y1),... , (xn, yn), type
x=c(x1,...,xn)
y=c(y1,...,yn)
model = lm(y ∼ x)
The third line defines model as lm(y ∼ x), which is the linear model that as-
sumes that y is a linear function of x plus a random error. Here is an example:
o > x = c(1,2,3,4,5,6,7)
o > y = c(3,2,5,6,4,8,9)
o > model = lm(y ∼ x)
o > model
After hitting return after the last line, one obtains the following output:
Call:
lm(formula = y ∼ x )
Coefficients:
(Intercept) x
The preceding says that the estimated regression line is y = 1.143 + 1.036x.
To obtain a plot of the estimated regression line, now type > abline(model ) and
hit return.
The result is:
Coefficients:
(Intercept) x1 x
Thus, the estimated regression equation is
Y = −169.5333 + 0.3933x1 + 0.8667x
o Logistic regression
The logistic regression model is written glm(y ∼ x). For instance, suppose an
experiment has been run 7 times, at levels x = (5, 9, 3, 12, 22, 24, 30) and with
results (0, 1, 0, 1, 0, 1, 1), with a response 1 meaning that experiment was a
success and a response 0 indicating that it was a failure. Then the estimated
logistics equation is obtained as follows:
x = c(5,9,3,12,22,24,30)
y = c(0,1,0,1,0,1,1)
model = glm(y ∼ x) > model
Pressing enter now yields the following output
Call: glm(formula = y∼ x )
Coefficients:
(Intercept) x
That is, the estimates of a and b in the logistics regression model 𝑝(𝑥) =
(
!"#$
!) (
!"#$
are 0.22205 and 0.
Chi-squared probabilities can be obtained from R. If you want the probability
that a chi-squared random variable with n degrees of freedom is less than x, just
type pchisq(x,n) and the probability will be given upon hitting return. If you
want the probability that it is greater than x, type 1 − pchisq(x, n). For in- stance,
the probability that a chi-squared with 10 degrees of freedom is greater than 15
is obtained from
1 − pchisq( 15 , 10 ) [ 1 ] 0. 1320619
Ifyouwantthevalueχ
n
,α
suchthat P(X>χ
n
,α
)=α,whenXisachi-squared
random variable with n degrees of freedom, just type qchisq( 1 − α, n). For in-
stance, we obtain χ
as follows: 12 ,. 04
qchisq( 1 −. 04 , 12 ) [ 1 ] 21. 78511
By the way, the reason we type qchisq( 1 − α, n) rather than qchisq(α, n) is that
qchisq(β,n) yields the 100 β percentile; that is, the probability that the chi-
square will be less than qchisq(β,n) is β, and so the probability that it will be
greater than qchisq(β, n) is 1 − β.