Enhancing Output

This page shows the use of value and variable labels, as well as how to save output in HTML and LaTeX. The books offer much more coverage of factors than simply value labels.

The practice data set is shown here. The programs and the data they use are also available for download here. Examples that are missing for Stata reflect the differences between the two books. We will get around to those when we write the second edition of R for Stata Users.


Value Labels

R

mydata$workshop <-
factor( mydata$workshop,
  levels = c(1, 2, 3, 4),
  labels = c("R", "SAS",
    "SPSS", "Stata") )

SAS

LIBNAME myLib 'C:myRfolder';
PROC FORMAT;
  VALUES workshop_f
    1="R"
    2="SAS"
    3="SPSS"
    4="Stata";
  VALUES $gender_f
    "m"="Male"
    "f"="Female";
  VALUES agreement
    1="Strongly Disagree"
    2="Disagree"
    3="Neutral"
    4="Agree"
    5="Strongly Agree";
RUN;

DATA myLib.mydata;
  SET myLib.mydata;
  FORMAT workshop workshop_f. gender gender_f.
    q1-q4 agreement.;
RUN;

SPSS

CD 'C:myRfolder'.
GET FILE='mydata.sav'.VARIABLE LEVEL workshop
(NOMINAL)
/q1 TO q4 (SCALE).VALUE LABELS
/workshop
1 'R'
2 'SAS'
3 'SPSS'
4 'Stata'
/q1 TO q4
1 'Strongly Disagree'
2 'Disagree'
3 'Neutral'
4 'Agree'
5 'Strongly Agree'.
SAVE OUTfile = "mydata.sav".

Stata

use c:myRfoldermydata, clear
label var q1 "The instructor was well prepared"
label var q2 "The instructor communicated well"
label var q3 "The course materials were helpful"
label var q4 "Overall, I found this workshop useful"
* summarize the q variables
sum q*

Variable Labels

R

# Filename: VarLabels.Rsetwd("c:/myRfolder")
load(file = "mydata.RData")
options(width = 63)
mydata
# Using the Hmisc label attribute.
library("Hmisc")
label(mydata$q1) <- "The instructor was well prepared."
label(mydata$q2) <- "The instructor communicated well."
label(mydata$q3) <- "The course materials were helpful."
label(mydata$q4) <- "Overall, I found this workshop useful."

# Hmisc describe function uses the labels.
describe( mydata[ ,3:6] )

# Buit-in summary function
# ignores the labels.
summary( mydata[ ,3:6] )

SAS

* Filename: VarLabels.sas ;LIBNAME myLib 'C:myRfolder';

DATA myLib.mydata;
  SET myLib.mydata ;
  LABEL
    Q1="The instructor was well prepared"
    Q2="The instructor communicated well"
    Q3="The course materials were helpful"
    Q4="Overall, I found this workshop useful";
RUN;

PROC FREQ; TABLES q1-q4; RUN;
RUN;

SPSS

* Filename: VarLabels.sps .CD 'C:myRfolder'.

GET FILE='mydata.sav'.
VARIABLE LABELS
Q1 "The instructor was well prepared"
Q2 "The instructor communicated well"
Q3 "The course materials were helpful"
Q4 "Overall, I found this workshop useful".

FREQUENCIES VARIABLES=q1 q2 q3 q4.
SAVE OUTFILE='mydata.sav'.

Stata

* Stata Program for Variable Labels use mydata
* Filename: VarLabels.douse c:myRfoldermydata, clear
label var q1 "The instructor was well prepared"
label var q2 "The instructor communicated well"
label var q3 "The course materials were helpful"
label var q4 "Overall, I found this workshop useful"

* Summarize the q variables
sum q*

Writing HTML & LaTeX

R

# Filename: FormattedOutput.Roptions(width = 60)
setwd("c:/myRfolder")
load("mydata.RData")
attach(mydata)
library("xtable")

# Formatting a Data Frame
print(mydata)
myXtable <- xtable(mydata)
class(myXtable)

print(myXtable, type = "html")
print(myXtable, type = "latex")

# Formatting a Linear Model
mymodel <- lm( q4 ~ q1 + q2 + q3, data = mydata)
myXtable <- xtable(mymodel)

label(myXtable) <- c("xtableOutput")
caption(myXtable) <- c("Linear model results formatted by xtable")
print(myXtable, type = "html")
print(myXtable, type = "latex")

SAS

In SAS, getting tabular results into your word processor or Web page is as easy as setting the style when you install it and then saving the output directly in the format you need. To get a subset of output, copy and paste works fine.

SPSS

In SPSS, getting tabular results into your word processor or Web page is as easy as setting the style when you install it and then saving the output directly in the format you need. To get a subset of output, copy and paste works fine.

Stata

use C:\myRfoldermydata, list
regress q4 q1 q2 q3
outtex

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.