require(knitr) opts_chunk$set( tidy=FALSE, # display code as typed size=“small” # slightly smaller font for code )
favstats(~ age, data=HELPrct)
## min Q1 median Q3 max mean sd n missing
## 19 30 35 40 60 35.65342 7.710266 453 0
tally(~ sex, data=HELPrct)
## sex
## female male
## 107 346
#graphing quantitative numeric variable
histogram(~age,data=HELPrct)
densityplot(~age,data=HELPrct)
bwplot(~age,data=HELPrct)
qqmath(~age,data=HELPrct)
freqpolygon(~age,data=HELPrct)
bargraph(~age,data=HELPrct)
bargraph(~sex, data=HELPrct) #graphing categorical variable
tally(homeless~sex,data=HELPrct)
## sex
## homeless female male
## homeless 40 169
## housed 67 177
bargraph(~sex,group=homeless, data=HELPrct,auto.key=TRUE)
cor(i1~age, data=HELPrct)
## [1] 0.2069538
xyplot(i1~age, data=HELPrct)
a1<-favstats(age~substance|sex,data=HELPrct)
a1
## sex min Q1 median Q3 max mean sd n missing
## 1 alcohol.female 23 33 37.0 45.0 58 39.16667 7.980333 36 0
## 2 cocaine.female 24 31 34.0 38.0 49 34.85366 6.195002 41 0
## 3 heroin.female 21 29 34.0 39.0 55 34.66667 8.035839 30 0
## 4 alcohol.male 20 32 38.0 42.0 58 37.95035 7.575644 141 0
## 5 cocaine.male 23 30 33.0 37.0 60 34.36036 6.889772 111 0
## 6 heroin.male 19 27 32.5 39.0 53 33.05319 7.973568 94 0
## 7 female 21 31 35.0 40.5 58 36.25234 7.584858 107 0
## 8 male 19 30 35.0 40.0 60 35.46821 7.750110 346 0
a2<-favstats(age~ racegrp, data=HELPrct)
a2
## racegrp min Q1 median Q3 max mean sd n missing
## 1 black 20 31.00 35 39.00 60 35.68246 7.083759 211 0
## 2 hispanic 21 28.25 32 36.25 55 33.20000 7.989789 50 0
## 3 other 22 30.00 34 40.50 48 34.96154 7.660187 26 0
## 4 white 19 30.00 36 42.00 58 36.46386 8.281152 166 0
bwplot(age~racegrp, data=HELPrct)#boxplot
a3<-mean(age~substance|sex,data=HELPrct,.format="table") #tabular form
a3
## substance sex mean
## 1 alcohol female 39.16667
## 2 alcohol male 37.95035
## 3 cocaine female 34.85366
## 4 cocaine male 34.36036
## 5 heroin female 34.66667
## 6 heroin male 33.05319
tally(sex~substance,data=HELPrct)
## substance
## sex alcohol cocaine heroin
## female 36 41 30
## male 141 111 94
summary(sex~substance,data=HELPrct)
## Length Class Mode
## 3 formula call
xyplot(i1~age,data=HELPrct)
bwplot(age~substance,data=HELPrct)
bwplot(age~substance|sex,data=HELPrct, .format="table")
densityplot(~age|sex,data=HELPrct,groups=substance, auto.key=TRUE)
data(Births78)
if (require(lattice)) {
xyplot(births ~ date, Births78)
xyplot(births ~ date, Births78, groups = wday, main="Births by Weekday ",t="l", auto.key=list(space="top",columns=4,
title="Weekday", cex.title=1,
lines=TRUE, points=FALSE))
}
## Warning in as.POSIXlt.POSIXct(x): unknown timezone 'zone/tz/2020a.1.0/
## zoneinfo/America/New_York'
## Some other generic functions, that will come in handy as we progress in the course
xpnorm( 700, mean=500, sd=100)
##
## If X ~ N(500, 100), then
##
## P(X <= 700) = P(Z <= 2) = 0.9772499
## P(X > 700) = P(Z > 2) = 0.02275013
## [1] 0.9772499
xpnorm( c(300, 700), mean=500, sd=100)
##
## If X ~ N(500, 100), then
##
## P(X <= 300) = P(Z <= -2) = 0.02275013
## P(X <= 700) = P(Z <= 2) = 0.97724987
## P(X > 300) = P(Z > -2) = 0.97724987
## P(X > 700) = P(Z > 2) = 0.02275013
## [1] 0.02275013 0.97724987
a<-lm(age~substance*sex, data=HELPrct)
plot(a)