R Notes for Classes

Some of R codes for classes are listed where [EMS] refers to Essential Medical Statistics.

[EMS] Chapter 3 Displaying the data

Shapes of frequency distributions R code:

[EMS] Chapter 4 Means, standard deviations and standard errors

## population mean and s.d.
set.seed(20)
mu <- 80 # the mean of population dbp
sigma <- 10 # the s.d. of population dbp
N <- 20000
Y <- rnorm(N, mean=mu, sd=sigma)
hist(Y, xlim=c(40, 120))
n <- 30 # the number of observation in each sample
y.sam <- sample(Y, size=n, replace=FALSE)

## estimation of population s.d
mean(Y)
mean(y.sam)
sd(Y)
sd(y.sam)
bin <- 10
par(mfrow=c(1,2))
hist(Y, xlim=c(40, 120))
hist(y.sam, bin, xlim=c(40, 120))

## sampling variation
n <- 30
n.trial <- 100 # the number of trials
y.sam.mean <- rep(NA, n.trial)
sampling.var <- function(n, n.trial){
for(i in 1:n.trial){
y.sam <- sample(Y, size=n, replace=FALSE)
y.sam.mean[i] <- mean(y.sam)
}
return(y.sam.mean)
}
y.sam.mean <- sampling.var(n = n, n.trial=n.trial)
par(mfrow=c(1,1))
plot(50.5, mean(Y), xlab="", col=2, pch=15, cex=1.3, xlim=c(1,100), ylim=c(60, 100))
abline(h=mu, col=3, lty=2)
points(1:n.trial, y.sam.mean)

## standard error
sd(y.sam.mean)
sigma/sqrt(n)

## sampling distribution
hist(y.sam.mean, bin, xlim=c(40, 120))
y.sam.mean.10 <- sampling.var(n = 10, n.trial=n.trial) # n=10
y.sam.mean.30 <- sampling.var(n = 30, n.trial=n.trial) # n=30
par(mfrow=c(1,2))
hist(y.sam.mean.10, bin, xlim=c(40, 120))
hist(y.sam.mean.30, bin, xlim=c(40, 120))

[EMS] Chapter 5 The normal distribution


Edit | Attach | Print version | History: r7 | r5 < r4 < r3 < r2 | Backlinks | View wiki text | Edit WikiText | More topic actions...
Topic revision: r4 - 13 Jan 2007, LeenaChoi
 

This site is powered by FoswikiCopyright © 2013-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback