A medical dataset to create graphs

For many of the examples in this book, we will use the following dataset. It gives medical data on 45 people: their names; their gender (a two-level categorical variable); their ethnicity (a four-level categorical variable, labeled 1, 2, and 3); the medical treatment they received (a three-level categorical variable with levels A, B, or C); their age band (a three-level categorical variable with levels Y, M, and E, standing for young, middle-aged, and elderly); their weight (body mass) before treatment (in kg) and weight (body mass) after treatment (in kg), their heights (in cm); whether they smoke (a two-level categorical variable with levels Y and N); whether they perform regular exercise (a two-level categorical variable with levels TRUE and FALSE); and finally, whether or not they recovered after treatment (a two-level categorical variable with levels 1 and 0). We read this dataset as an object called T. The syntax to read this dataset is given in the code file of this chapter. You can either copy and paste it directly from the text file, or save the Patients.csv file in a folder and read the data into R using read.csv(). Then, use the following command:

head(T)

We get the following output:

A medical dataset to create graphs

Again, we attach an object using the attach() command:

attach(T)

Let's now create a similar regression plot for the height and weight variables of our medical dataset. This job is done using the following syntax:

plot(WEIGHT_1, HEIGHT,pch=16,xlab="WEIGHT BEFORE TREATMENT (kg)",ylab="HEIGHT (cm)", main="HEIGHT VS. WEIGHT", cex = 0.8, cex.lab = 1.5, cex.main = 1.6, xlim=c(0,150), ylim=c(100,200))

Here is our graph:

A medical dataset to create graphs

Now, we create the regression model using lm():

mod <- lm(HEIGHT ~ WEIGHT_1)

mod

We get the following output:

Call:
lm(formula = HEIGHT ~ WEIGHT_1)

Coefficients:
(Intercept)     WEIGHT_1  
   119.7836       0.6737  

The intercept is approximately 119.78 and the slope (the coefficient of the weight variable) is approximately 0.67. It gives the change in height for a weight change of one unit. Now we plot the regression line:

abline(lm(HEIGHT ~ WEIGHT_1))

The following is the graph with the regression line:

A medical dataset to create graphs

We will not draw the residuals this time, as this graph would look very cluttered if we did. However, by now you should know how to draw both regression lines and residuals.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset