Wednesday, April 29, 2009

Nonlnear Models - GLS


### Indometh

ols<-nls(conc~SSbiexp(time,b1,b2,b3,b4),
data=Indometh[Indometh$Subject==1,])
beta<-coef(ols)

for(i in 1:10){
oldbeta<-beta
out<-nls(conc~SSbiexp(time,b1,b2,b3,b4),
data=Indometh[Indometh$Subject==1,],
weights=1/SSbiexp(time,beta[1],beta[2],beta[3],beta[4])^2)
beta<-coef(out)
}

BETA<-matrix(rep(0,6*4),6)
for(i in 1:6){
BETA[i,]<-coef(nls(conc~SSbiexp(time,b1,b2,b3,b4),
data=Indometh[Indometh$Subject==i,]))
}
colnames(BETA)<-c('b1','b2','b3','b4')


### Theoph
coplot(conc~Time | Subject, data=Theoph)
nls(conc~SSfol(Dose,Time,lKe,lKa,lCl),data=Theoph)
ols<-nls(conc~SSfol(Dose,Time,lKe,lKa,lCl),
data=Theoph[Theoph$Subject==1,])
beta<-coef(ols)

BETA<-matrix(rep(0,12*3),12)
for(i in 1:max(as.numeric(Theoph$Subject)))
BETA[i,]<-coef(nls(conc~SSfol(Dose,Time,b1,b2,b3),
data=Theoph[Theoph$Subject==i,]))


Thursday, April 23, 2009

Poisson Regression


melanoma<-read.csv("U:/data/R/datasets/melanoma.csv")

out<-glm(cases~age+region, family=poisson(link="log"),
offset=log(total), data=melanoma)
out1<-glm(cases~age, family=poisson(link="log"),
offset=log(total), data=melanoma)
out2<-glm(cases~region, family=poisson(link="log"),
offset=log(total), data=melanoma)

# type 3
anova(out1,out)
anova(out2,out)

# melanoma
Obs age region cases total

1 35-44 south 75 220407
2 45-54 south 68 198119
3 55-64 south 63 134084
4 65-74 south 45 70708
5 75+ south 27 34233
6 <35 south 64 1074246
7 35-44 north 76 564535
8 45-54 north 98 592983
9 55-64 north 104 450740
10 65-74 north 63 270908
11 75+ north 80 161850
12 <35 north 61 2880262

Saturday, April 11, 2009

Datasets

# PK
xyplot(conc~time|Subject, data=Indometh) # IV
xyplot(conc~Time|Subject, data=Theoph) # oral

# Linear
plot(dist~speed, data=cars)
plot(eruptions~waiting, data=faithful)
xyplot(weight~Time | Diet, data=ChickWeight)
lme(weight~Time+Diet, random=~1+Time |Chick, data=ChickWeight)
xyplot(height~age|Seed, data=Loblolly)

# Latin-square
OrchardSprays

# ANOVA
boxplot(weight~group, data=PlantGrowth)
boxplot(weight~feed, data=chickwts)

boxplot(breaks~wool+tension, data=warpbreaks)


# Nonlinear
xyplot(uptake~conc|Plant, data=CO2)
xyplot(density ~ conc | Run, data=DNase)
xyplot(rate~conc|state, data=Puromycin)
xyplot(circumference~age|Tree, data=Orange)
plot(pressure~temperature, data=pressure)

# multivariate
attitude
trees

# logistic regression
esoph
glm(cbind(ncases, ncontrols)~agegp+tobgp*alcgp,data=esoph,family=binomial())

# paired t-test
t.test(extra~group, paired=TRUE, data=sleep)

##### MASS
# Survival
Melanoma
VA
gehan
leuk

xyplot(BPchange~Dose | Animal+Treatment, data=Rabbit)
xyplot(size~Time | treat, data=Sitka)

# logistic regression
birthwt

# Poisson Regression
boxplot(log(y)~limit, data=Traffic)
summary(glm(y~limit, data=Traffic, family=poisson))
summary(glm(y~day+limit, data=Traffic, family=poisson))

# nonlinear
plot(steam)

# two-way anova
genotype

# ANACOVA
anova(lm(Postwt~Prewt+Treat, data=anorexia))

# paired t-test
write.csv(anorexia[anorexia$Treat=="CBT"2:3],"u:/data/R/datasets/paired_CBT.csv",row.names=FALSE)