astudent’s guide to r 71
We observe that the cocaine and heroin groups are sig-
nificantly less likely to be homeless than alcohol involved
subjects, after controlling for age. (A similar result is seen
when considering just homeless status and substance.)
> tally(~ homeless | substance, format="percent"margins=TRUEdata=HELPrct)
substance
homeless
alcohol cocaine heroin
homeless
58.2
38.8
37.9
housed
41.8
61.2
62.1
Total
100.0
100.0
100.0
Pdf to powerpoint converter online - software control dll:C# Create PDF from PowerPoint Library to convert pptx, ppt to PDF in C#.net, ASP.NET MVC, WinForms, WPF
Online C# Tutorial for Creating PDF from Microsoft PowerPoint Presentation
www.rasteredge.com
Pdf to powerpoint converter online - software control dll:VB.NET Create PDF from PowerPoint Library to convert pptx, ppt to PDF in vb.net, ASP.NET MVC, WinForms, WPF
VB.NET Tutorial for Export PDF file from Microsoft Office PowerPoint
www.rasteredge.com
software control dll:Online Convert PowerPoint to PDF file. Best free online export
Online Powerpoint to PDF Converter. Download Free Trial. Convert a PPTX/PPT File to PDF. Just upload your file by clicking on the blue
www.rasteredge.com
software control dll:XDoc.Converter for .NET, Support Documents and Images Conversion
Convert PowerPoint to ODP. Convert to Tiff. Convert Word, Excel and PDF to image. Next Steps. Download and try Converter for .NET with online support. See Pricing
www.rasteredge.com
9
Survival Time Outcomes
Extensive support for survival (time to event) analysis is
available within the
survival
package.
9.1 Kaplan-Meier plot
> require(survival)
> fit <- survfit(Surv(dayslink, linkstatus) ~ treat,
data=HELPrct)
> plot(fit, conf.int=FALSElty=1:2lwd=2,
xlab="time (in days)", ylab="P(not linked)")
> legend(200.4legend=c("Control""Treatment"),
lty=c(1,2), lwd=2)
> title("Product-Limit Survival Estimates (time to linkage)")
0
100
200
300
400
0.00.20.40.60.81.0
time (in days)
P(not linked)
Control
Treatment
Product−Limit Survival Estimates (time to linkage)
software control dll:C#: How to Use SDK to Convert Document and Image Using XDoc.
You may use our converter SDK to easily convert PDF, Word, Excel, PowerPoint, Tiff, and Dicom files to raster images like Jpeg, Png, Bmp and Gif.
www.rasteredge.com
software control dll:RasterEdge XDoc.PowerPoint for .NET - SDK for PowerPoint Document
Add image to specified position on PowerPoint page. Next Steps. Download Free Trial Download and try PDF for .NET with online support. See Pricing
www.rasteredge.com
74 horton, kaplan, pruim
We see that the subjects in the treatment (Health Eval-
uation and Linkage to Primary Care clinic) were signif-
icantly more likely to link to primary care (less likely to
“survive”) than the control (usual care) group.
9.2 Cox proportional hazards model
> require(survival)
> summary(coxph(Surv(dayslink, linkstatus) ~ age + substance,
data=HELPrct))
Call:
coxph(formula = Surv(dayslink, linkstatus) ~ age + substance,
data = HELPrct)
n= 431, number of events= 163
(22 observations deleted due to missingness)
coef exp(coef) se(coef)
z Pr(>|z|)
age
0.00893
1.00897
0.01026
0.87
0.38
substancecocaine
0.18045
1.19775
0.18100
1.00
0.32
substanceheroin
-0.28970
0.74849
0.21725 -1.33
0.18
exp(coef) exp(-coef) lower .95 upper .95
age
1.009
0.991
0.989
1.03
substancecocaine
1.198
0.835
0.840
1.71
substanceheroin
0.748
1.336
0.489
1.15
Concordance= 0.55
(se = 0.023 )
Rsquare= 0.014
(max possible= 0.988 )
Likelihood ratio test= 6.11
on 3 df,
p=0.106
Wald test
= 5.84
on 3 df,
p=0.12
Score (logrank) test = 5.91
on 3 df,
p=0.116
Neither age nor substance group was significantly
associated with linkage to primary care.
software control dll:VB.NET PDF - Convert PDF Online with VB.NET HTML5 PDF Viewer
All Formats. XDoc.HTML5 Viewer. XDoc.Windows Viewer. XDoc.Converter. View & Process. Adobe PDF. XDoc.PDF. Scanning. XDoc.Word. XDoc.Excel. XDoc.PowerPoint. Barcoding
www.rasteredge.com
software control dll:VB.NET PDF- View PDF Online with VB.NET HTML5 PDF Viewer
All Formats. XDoc.HTML5 Viewer. XDoc.Windows Viewer. XDoc.Converter. View & Process. Adobe PDF. XDoc.PDF. Scanning. XDoc.Word. XDoc.Excel. XDoc.PowerPoint. Barcoding
www.rasteredge.com
10
More than Two Variables
10.1 Two (or more) way ANOVA
We can fit a two (or more) way ANOVA model, without
or with an interaction, using the same modeling syntax.
> median(cesd ~ substance | sex, data=HELPrct)
alcohol.female cocaine.female
heroin.female
alcohol.male
40.0
35.0
39.0
33.0
cocaine.male
heroin.male
female
male
29.0
34.5
38.0
32.5
> bwplot(cesd ~ subgrp | sex, data=HELPrct)
cesd
0
10
20
30
40
50
60
A
C
H
l
l
l
female
A
C
H
l
l
l
l
male
> summary(aov(cesd ~ substance + sex, data=HELPrct))
Df Sum Sq Mean Sq F value
Pr(>F)
substance
2
2704
1352
9.27 0.00011
software control dll:C# HTML5 PDF Viewer SDK to convert and export PDF document to
All Formats. XDoc.HTML5 Viewer. XDoc.Windows Viewer. XDoc.Converter. View & Process. Adobe PDF. XDoc.PDF. Scanning. XDoc.Word. XDoc.Excel. XDoc.PowerPoint. Barcoding
www.rasteredge.com
software control dll:DocImage SDK for .NET: Web Document Image Viewer Online Demo
Try Online Demo Now. Please click Browse to upload a file to display in web viewer. Suppported files are Word, Excel, PowerPoint, PDF, Tiff, Dicom and main
www.rasteredge.com
76 horton, kaplan, pruim
sex
1
2569
2569
17.61 3.3e-05
Residuals
449
65515
146
> summary(aov(cesd ~ substance
*
sex, data=HELPrct))
Df Sum Sq Mean Sq F value
Pr(>F)
substance
2
2704
1352
9.25 0.00012
sex
1
2569
2569
17.57 3.3e-05
substance:sex
2
146
73
0.50 0.60752
Residuals
447
65369
146
There’s little evidence for the interaction, though there are
statistically significant main effects terms for
substance
group and
sex
.
> xyplot(cesd ~ substance, groups=sex, lwd=2,
auto.key=list(columns=2, lines=TRUE, points=FALSE),
type='a', data=HELPrct)
substance
cesd
0
10
20
30
40
50
60
alcohol
cocaine
heroin
female
male
10.2 Multiple regression
Multiple regression is a logical extension of the prior
commands, where additional predictors are added. This
allows students to start to try to disentangle multivariate
relationships.
We tend to introduce multiple
linear regression early in our
courses, as a purely descriptive
technique, then return to it
regularly. The motivation for
this is described at length in
the companion volume Start
Modeling with R.
Here we consider a model (parallel slopes) for depres-
sive symptoms as a function of Mental Component Score
(MCS), age (in years) and sex of the subject.
astudent’s guide to r 77
> lmnointeract <- lm(cesd ~ mcs + age + sex, data=HELPrct)
> msummary(lmnointeract)
Estimate Std. Error t value Pr(>|t|)
(Intercept)
53.8303
2.3617
22.79
<2e-16
mcs
-0.6548
0.0336
-19.50
<2e-16
age
0.0553
0.0556
1.00
0.3200
sexmale
-2.8993
1.0137
-2.86
0.0044
Residual standard error: 9.09 on 449 degrees of freedom
Multiple R-squared:
0.476,Adjusted R-squared:
0.473
F-statistic:
136 on 3 and 449 DF,
p-value: <2e-16
We can also fit a model that includes an interaction be-
tween MCS and sex.
> lminteract <- lm(cesd ~ mcs + age + sex + mcs:sex, data=HELPrct)
> msummary(lminteract)
Estimate Std. Error t value Pr(>|t|)
(Intercept)
55.3906
2.9903
18.52
<2e-16
mcs
-0.7082
0.0712
-9.95
<2e-16
age
0.0549
0.0556
0.99
0.324
sexmale
-4.9421
2.6055
-1.90
0.058
mcs:sexmale
0.0687
0.0807
0.85
0.395
Residual standard error: 9.09 on 448 degrees of freedom
Multiple R-squared:
0.477,Adjusted R-squared:
0.472
F-statistic:
102 on 4 and 448 DF,
p-value: <2e-16
> anova(lminteract)
Analysis of Variance Table
Response: cesd
Df Sum Sq Mean Sq F value Pr(>F)
mcs
1
32918
32918
398.27 <2e-16
age
1
107
107
1.29 0.2563
sex
1
676
676
8.18 0.0044
mcs:sex
1
60
60
0.72 0.3952
Residuals 448
37028
83
> anova(lmnointeract, lminteract)
Analysis of Variance Table
78 horton, kaplan, pruim
Model 1: cesd ~ mcs + age + sex
Model 2: cesd ~ mcs + age + sex + mcs:sex
Res.Df
RSS Df Sum of Sq
F Pr(>F)
1
449 37088
2
448 37028
1
59.9 0.72
0.4
There is little evidence for an interaction effect, so we
drop this from the model.
10.2.1 Visualizing the results from the regression
The
makeFun()
and
plotFun()
functions from the
mosaic
package can be used to display the predicted values from
aregression model. For this example, we might display
the predicted CESD values for a range of MCS (mental
component score) values a hypothetical 36 year old male
and female subject might have from the parallel slopes
(no interaction) model.
> lmfunction <- makeFun(lmnointeract)
We can now plot the predicted values separately for
male and female subjects over a range of MCS (mental
component score) values, along with the observed data
for all of the 36 year olds.
> xyplot(cesd ~ mcs, groups=sex, auto.key=TRUE,
data=filter(HELPrct, age==36))
> plotFun(lmfunction(mcs, age=36sex="male") ~ mcs,
xlim=c(0, 60), lwd=2, ylab="predicted CESD", add=TRUE)
> plotFun(lmfunction(mcs, age=36sex="female") ~ mcs,
xlim=c(0, 60), lty=2, lwd=3, add=TRUE)
astudent’s guide to r 79
mcs
cesd
10
20
30
40
50
20
30
40
50
60
l
l
l
female
male
l
10.2.2 Coefficient plots
It is sometimes useful to display a plot of the coefficients
for a multiple regression model (along with their associ-
ated confidence intervals).
> mplot(lmnointeract, rows=-1which=7)
95% confidence intervals
estimate
coefficient
sexmale
age
mcs
−5
−4
−3
−2
−1
0
Darker dots indicate regression
coefficients where the 95%
confidence interval does not
include the null hypothesis
value of zero.
Caution!
Be careful when fitting re-
gression models with missing
values (see also section 13.11).
10.2.3 Residual diagnostics
It’s straightforward to undertake residual diagnostics
for this model. We begin by adding the fitted values and
residuals to the dataset.
The
mplot()
function can also
be used to create these graphs.
Here we are adding two new
variables into an existing
dataset. It’s often a good
practice to give the resulting
dataframe a new name.
80 horton, kaplan, pruim
> HELPrct <- mutate(HELPrct,
residuals = resid(lmnointeract),
pred = fitted(lmnointeract))
> histogram(~ residuals, xlab="residuals"fit="normal",
data=HELPrct)
residuals
Density
0.00
0.01
0.02
0.03
0.04
−30
−20
−10
0
10
20
We can identify the subset of observations with ex-
tremely large residuals.
> filter(HELPrct, abs(residuals) > 25)
age anysubstatus anysub cesd d1 daysanysub dayslink drugrisk e2b
1
43
0
no
16 15
191
414
0
NA
2
27
NA
<NA>
40
1
NA
365
3
2
female
sex g1b homeless i1 i2
id indtot linkstatus link
mcs
pcs
1
0 male
no homeless 24 36
44
41
0
no 15.9 71.4
2
0 male
no homeless 18 18 420
37
0
no 57.5 37.7
pss
_
fr racegrp satreat sexrisk substance treat subgrp residuals
1
3
white
no
7
cocaine
yes
C
-26.9
2
8
white
yes
3
heroin
no
H
25.2
pred
1 42.9
2 14.8
> xyplot(residuals ~ pred, ylab="residuals"cex=0.3,
xlab="predicted values", main="predicted vs. residuals",
type=c("p", "r", "smooth"), data=HELPrct)
Documents you may be interested
Documents you may be interested