PNG <- TRUE
set.seed(768)
x <- rnorm(20,mean=20,sd=2.5)
y <- rnorm(20,mean=22,sd=2.3)
if (PNG) png("ex1.png")
boxplot(x,y)
points(c(rep(1,20),rep(2,20)),c(x,y),col='gray50')
points(c(1,2),c(mean(x),mean(y)),pch='x',cex=2,col=c('blue','red'))
if (PNG) dev.off()
t.test(x,y,var.equal=T) t.test(x,y,var.equal=F)
a <- read.table("sommeil.txt",header=T)
my.summary <- function(x,digits=2) {
tmp <- c(summary(x),sd(x))
names(tmp)[7] <- "Sd"
return(round(tmp,digits))
}
attach(a)
tapply(temps,med,my.summary)
if (PNG) png("ex2.png")
par(mfrow=c(1,2))
boxplot(temps~med,col="gray",main="n=10",ylab="Temps (h)",xlab="M",names=c("1","2"))
plot(temps[med==1],temps[med==2],pch=16,main="n=10",xlim=c(2,12),ylim=c(2,12),xlab="M1",ylab="M2")
abline(0,1)
if (PNG) dev.off()
t.test(temps[med==1],temps[med==2],var.equal=T, paired=T)
t.test(temps[med==1],temps[med==2],var.equal=T)
nsuj <- 60
effectsize <- 0.5
subject <- gl(nsuj,1)
group <- gl(2,nsuj/2)
scores <- 1+rnorm(nsuj)+((0:(nsuj-1))%/%(nsuj/2))*effectsize
par(mfcol=c(1,3))
stripchart(scores~group,vert=T,method='jitter')
plot.design(scores~subject+group)
boxplot(scores~group)
by(scores,group,mean)
by(scores,group,summary)
t.test(scores~group)
if (PNG) png("ex3-1.png")
par(mfrow=c(1,2))
plot(scores[group==1]-mean(scores[group==1]))
abline(h=0,lty=2)
plot(scores[group==2]-mean(scores[group==2]))
abline(h=0,lty=2)
if (PNG) dev.off()
if (PNG) png("ex3-2.png")
boxplot(scores~group)
if (PNG) dev.off()
if (PNG) png("ex3-3.png")
par(mfrow=c(1,2))
qqnorm(scores[group==1])
qqline(scores[group==1])
qqnorm(scores[group==2])
qqline(scores[group==2])
if (PNG) dev.off()
ns <- c(15,28,10,20,35)
n <- length(ns)
group <- factor(rep(1:n,ns),labels=paste("g",1:n,sep=""))
data <- rnorm(length(group),mean=100+(as.numeric(group)-2)^2)
if (PNG) png("ex4.png")
par(mfrow=c(1,3))
plot(data~group)
stripchart(data~group,method="jitter",vertical=T)
plot.design(data~group)
if (PNG) dev.off()
model2 <- aov(data~group)
model2
summary(model2)
if (PNG) png("ex5.png")
par(mfrow=c(2,2))
plot(model2)
if (PNG) dev.off()
a <- read.table('pedago.txt',header=T)[-1]
names(a) <- c("pedago","notes")
attach(a)
boxplot(notes~pedago)
stripchart(notes~pedago,method='stack',vertical=T)
(moy <- as.numeric(tapply(notes,pedago,mean)))
points(1:4,moy,pch="X",col='blue',cex=2)
lines(1:4,moy,col='blue',lwd=2)
tapply(notes,pedago,summary)
tapply(notes,pedago,sd)
m <- aov(notes~pedago)
summary(m)
pairwise.t.test(notes,pedago)
TukeyHSD(m)
plot(TukeyHSD(m))
x1 <- rnorm(25)+0.5*runif(25)
x2 <- rnorm(25)+0.25*runif(25)
x3 <- rnorm(25)+1.5*runif(25)
x4 <- rnorm(25)+2.5*runif(25)
x <- c(x1,x2,x3,x4)
a <- gl(2,50,100)
b <- gl(2,25,100)
levels(a) <- c("a1","a2")
levels(b) <- c("b1","b2")
plot(x~factor(a:b))
gmean <- tapply(x,list(a,b),mean)
model <- aov(x~a*b)
summary(model)
a <- read.table("tab13-2.dat",header=F)
a$V1 <- factor(a$V1,labels=c("ages","jeunes"))
a$V2 <- factor(a$V2,labels=c("addition","rimes","adjectifs","images","controle"))
summary(a)
a.means <- with(a, tapply(V3,list(Age=V1,Groupe=V2),mean))
a.sd <- with(a, tapply(V3,list(Age=V1,Groupe=V2),sd))
my.barplot <- function(data, err, type, ...) {
tmp <- barplot(data, ...)
if (length(err) != length(data))
stop("la longueur de 'data' et 'err' ne correspond pas")
for (i in 1:length(err))
arrows(tmp[i],data[i]-err[i],tmp[i],data[i]+err[i],code=3,angle=90,length=0.08)
}
my.barplot(a.means,a.sd,beside=T,ylab="Nb. mots",xlab="Condition",ylim=c(0,26),
names.arg=levels(a$V2),legend.text=levels(a$V1),cex.axis=.7,cex.names=.7,cex.lab=.7)
a.aov <- aov(V3 ~ V1 * V2,data=a)
summary(a.aov)
plot(a.aov)
model.tables(a.aov,se=T)
interaction.plot(a[,2],a[,1],a[,3])
suj <- as.factor(rep(seq(1,10),3))
c1 <- factor(c(rep(1,10),rep(2,10),rep(3,10)),labels=c("1jr","2jr","3jr"))
v <- rnorm(30,mean=20,sd=2)+as.numeric(c1)*rnorm(30,5)
boxplot(v~c1)
interaction.plot(c1,suj,v)
anova(lm(v~suj+c1))
summary(aov(v~suj+c1))
summary(aov(v~c1+Error(suj/c1)))
library(nlme)
summary(lme(v~c1,random=~1|suj))
a <- rnorm(100)
b <- 2*a+rnorm(100)
plot(b~a)
r <- lm(b~a)
anova(r)
summary(r)
abline(r)
plot(r)
a <- read.table("moineaux.txt",header=T)
summary(a)
sd(a)
m <- lm(Y~X,data=a)
summary(m)
plot(a$Y~a$X,xlab="Age (jours)",ylab="Longueur (cm)")
abline(m)
plot(m)
anova(m)
library(car) Anova(m,type="III")
a <- read.csv("rli_voix.csv",header=T,sep=";",dec=",")
str(a)
head(a)
attach(a)
par(mfrow=c(1,2))
hist(ham,prob=T)
lines(density(ham,na.rm=T),col='red')
hist(af0s,prob=T)
lines(density(af0s,na.rm=T),col='red')
pairs(a)
summary(lm(ham~af0s))
summary(lm(ham~af0s+tricyc+serot+neurol+bz))
summary(lm(ham~af0s+tricyc+serot+neurol+bz+tyrer))
i <- 1:100
Z1 <- i
Z2 <- i^2
Z3 <- i^3
Z4 <- i^4
Z5 <- sqrt(i)
Z6 <- 1/i
Z7 <- log(i)
eps <- 20*rnorm(100,0)
Y <- 5-0.03*Z2+0.0002*Z3-3*Z7+eps
j <- 1:150
YY <- 5-0.03*(j^2)+0.0002*(j^3)-3*log(j)
if (PNG) png("sel-model-4.png")
plot(j,YY,type="l",xlim=c(-10,160),ylim=c(-150,50))
points(i,Y)
require(car)
y.lm7 <- lm(Y~Z1+Z2+Z3+Z4+Z5+Z6+Z7)
new <- data.frame(Z1=j,Z2=j^2,Z3=j^3,Z4=j^4,Z5=sqrt(j),Z6=1/j,Z7=log(j))
y.pred7 <- predict(y.lm7,new)
lines(j,y.pred7,lty="dashed")
Anova(y.lm7)
Anova(lm(Y~Z1+Z2+Z3+Z4+Z5+Z7))
Anova(lm(Y~Z1+Z2+Z3+Z4+Z5))
Anova(lm(Y~Z1+Z2+Z3+Z4))
Anova(lm(Y~Z2+Z3+Z4))
Anova(lm(Y~Z2+Z3))
y.lm2 <- lm(Y~Z2+Z3)
y.pred2 <- predict(y.lm2,new)
lines(j,y.pred2,lty="dashed",col="red")
library(leaps)
Z <- matrix(c(Z1,Z2,Z3,Z4,Z5,Z6,Z7),ncol=7)
colnames(Z) <- c("Z1","Z2","Z3","Z4","Z5","Z6","Z7")
r <- leaps(Z,Y)
r$whi
r$Cp
t <- (r$Cp==min(r$Cp))
colnames(Z)[r$whi[t]]
y.pred.cp <- predict(lm(Y~Z1+Z4),new)
lines(j,y.pred.cp,lty="dashed",col="blue")
require(MASS)
ZZ <- as.data.frame(Z)
y.lm <- lm(Y~.,data=ZZ)
y.bic <- stepAIC(y.lm,k=log(100))
y.aic <- stepAIC(y.lm,k=2,trace=FALSE)
y.bic
y.aic
y.pred.aic <- predict(lm(Y~Z1+Z2+Z3+Z5+Z7),new)
lines(j,y.pred.aic,lty="dashed",col="green")
y.pred.bic <- predict(lm(Y~Z2+Z3),new)
points(j,y.pred.bic,col="cyan")
legend("bottomleft",c("True Model","Complete Model","Stepwise desc.","Cp Mallows","AIC","BIC"),
lty=c(1,rep(2,4)),col=c(rep("black",2),"red","blue","green","cyan"),bty="n")
if (PNG) dev.off()