
library(foreign)
#library(Rcmdr)
library(nlme)
library(car)

data <- read.spss("C:/Dokumente und Einstellungen/Peter/Desktop/Mixed Maxewell&Delaney.sav", use.value.labels=TRUE, max.value.labels=5, to.data.frame=TRUE)
summary(data)
names(data)
data$FTIME<-as.factor(data$TIME)
data$SEX<-as.factor(data$SEX)
data$FSUBJECT<-as.factor(data$SUBJECT)
class(data$TIME);class(data$FTIME);class(data$SEX);class(data$SUBJECT)

scatterplot(IQ~TI1 | SEX, reg.line=FALSE, smooth=TRUE, labels=FALSE, boxplots='xy', span=0.5, by.groups=TRUE, data=data)
tapply(data$IQ, list(SEX=data$SEX, TIME=data$TIME), mean, na.rm=TRUE)

data.gr <- groupedData(IQ ~ TIME | SUBJECT, data=data,outer= ~ SEX)
plot(data.gr[data$SUBJECT<20,])
plot(data.gr[data$SUBJECT<20,],outer=T)

glm.1 <- aov(IQ ~ FTIME*SEX + Error(FSUBJECT/FTIME),data=data)
summary(glm.1)
glm.21 <- aov(IQ ~ (TIME+I(TIME^2)+I(TIME^3))*SEX + Error(FSUBJECT/(TIME+I(TIME^2)+I(TIME^3))),data=data)
summary(glm.21)
glm.22 <- aov(IQ ~ (TIME+I(TIME^2)+I(TIME^3))*SEX + Error(FSUBJECT),data=data)
summary(glm.22)

lme1 <- lme(fixed = IQ ~ FTIME*SEX, data = data.gr, random = ~ 1 | SUBJECT)
lme1
anova(lme1)
lme22 <- lme(fixed = IQ ~ (TIME+I(TIME^2)+I(TIME^3))*SEX, data = data.gr, random = ~ 1 | SUBJECT)
lme22
anova(lme22) 