library(car)

#########################################
### Self-Reports of Height and Weight ###
#########################################

#The Davis data frame has 200 rows and 5 columns. 
#The subjects were men and women engaged in
#regular exercise. There are some missing data.

head(Davis)

scatterplot(weight ~ repwt, data=Davis, smooth=FALSE)
scatterplot(weight ~ repwt, data=Davis, smooth=TRUE, id.n=1)
scatterplot(weight[-12] ~ repwt[-12], data=Davis, smooth=TRUE, id.n=1)

davis.mod <- lm(weight ~ repwt, data=Davis)
summary(davis.mod)
confint(davis.mod)

davis.mod.2 <- update(davis.mod, subset=-12)
summary(davis.mod.2)  

cbind(Original=coef(davis.mod), SemCaso12=coef(davis.mod.2))

###########################################
###########################################

################################################
### Prestige of Canadian Occupations in 1971 ###
################################################

head(Prestige)

scatterplot(prestige ~ education, data=Prestige, smooth=FALSE)
scatterplot(prestige ~ income, data=Prestige, smooth=FALSE)
scatterplot(prestige ~ women, data=Prestige, smooth=FALSE)

prestige.mod <- lm(prestige ~ education + income + women, data=Prestige)
summary(prestige.mod)

prestige.mod <- lm(prestige ~ education + log2(income) + women, data=Prestige)
summary(prestige.mod)

#####################################
#####################################

#################################################
### Methods of Teaching Reading Comprehension ###
#################################################

#Students were randomly assigned to one of three experimental groups.

set.seed(1234)
some(Baumann)
xtabs(~ group, data=Baumann)

with(Baumann, tapply(post.test.3, group, mean))
with(Baumann, tapply(post.test.3, group, sd))

plot(post.test.3 ~ group, data=Baumann, xlab="Grupo",
     ylab="Nota")

baum.mod.1 <- lm(post.test.3 ~ group, data=Baumann)
summary(baum.mod.1)

summary(update(baum.mod.1, . ~ . - group + relevel(group, ref="DRTA")))

###############################
###############################

############################################
### Survey of Labour and Income Dynamics ###
############################################

set.seed(12345) 
some(SLID)


boxplot(wages ~ sex, data=SLID)

scatterplot(wages ~ education, data=SLID)
scatterplot(log(wages) ~ education, data=SLID)
scatterplot(log(wages) ~ education, data=SLID,education >= 6)

scatterplot(wages ~ age, data=SLID)
scatterplot(wages ~ age, data=SLID,subset = age >= 18 & age <= 65)


mod.slid.0 <- lm(log(wages) ~ sex + education + age, data=SLID)
summary(mod.slid.0)

mod.slid.1 <- lm(log(wages) ~ sex + education + age, data=SLID,
                 subset = age >= 18 & age <= 65 & education >= 6)
summary(mod.slid.1)

Anova(mod.slid.1)

scatterplot(education ~ age, data=SLID)
mod.slid.2 <- lm(education ~ age , data=SLID)
summary(mod.slid.2)

scatterplot(education ~ age, data=SLID)
mod.slid.3 <- lm(education ~ poly(age,2) , data=SLID)
summary(mod.slid.3)


mod.slid.4 <- lm(log(wages) ~ sex + education , data=SLID)
summary(mod.slid.4)

mod.slid.5 <- lm(log(wages) ~ sex + education + age, data=SLID)
summary(mod.slid.5)

mod.slid.6 <- lm(log(wages) ~ sex + age, data=SLID)
summary(mod.slid.6)

coef(mod.slid.4)
coef(mod.slid.5)
coef(mod.slid.6)


mod.slid.7 <- lm(log(wages) ~ sex + education + poly(age, 2), data=SLID,
                 subset = age >= 18 & age <= 65 & education >= 6)
summary(mod.slid.7)
summary(mod.slid.1)

#################################
#################################

##############################################################################
### Data on transaction times in branch offices of a large Australian bank ###
##############################################################################

# Data on transaction times in branch offices of a large Australian bank.

set.seed(12345) 

some(Transact)

scatterplot(time ~ t1, data=Transact)
scatterplot(time ~ t2, data=Transact)


summary(trans.mod.0 <- lm(time ~ t1 , data=Transact))
summary(trans.mod.1 <- lm(time ~ t2, data=Transact))
summary(trans.mod.2 <- lm(time ~ t1 + t2, data=Transact))
summary(trans.mod.3 <- lm(time ~ t2 + t1, data=Transact))




