Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data Analysis Your task is to load the patients data and write a script that doe

ID: 3209802 • Letter: D

Question

Data Analysis Your task is to load the patients data and write a script that does the following 1. Find the mean and median ages of smokers and of non-smokers 2. Find how many people from the County General Hospital rate themselves to be in Excellent health. 3. Make a scatterplot of weight vs. height, where the data for smokers and non-smokers are different colors. Include a legend saying which is which. 4. Write the answers to the first two parts to a .txt file. 5. Save the scatterplot as a pdf filc. Tips: . Vectorized notation and filtering are your friends. . If for some reason you don't have patients on Matlab, the patients.mat file will be provided on the class site. . As always, make sure your code is commented well enough to be legible.

Explanation / Answer

Calculate the mean heights by gender for groups of patients and display the results.

Load patient heights and genders from the data file patients.mat.

Specify groups by gender with findgroups.

Split Height into groups specified by G. Calculate the mean height by gender. The first row of the output argument is the mean height of the female patients, and the second row is the mean height of the male patients.

Split Two Data Variables and Apply Function

Try This Example

Calculate the variances of the differences in blood pressure readings for groups of patients, and display the results. The blood pressure readings are contained in two data variables. To calculate the differences, use a function that takes two input arguments.

Load blood pressure readings and smoking data for 100 patients from the data file patients.mat.

Define func as a function that calculates the variances of the differences between systolic and diastolic blood-pressure readings for smokers and nonsmokers.func requires two input arguments.

Use findgroups and splitapply to split the patient data into groups and calculate the variances of the differences. findgroups also returns group identifiers in smokers. The splitapply function calls func once per group, with Systolic and Diastolic as the two input arguments.

Create a table that contains the variances of the differences, with the number of patients in each group.

Return Nonscalar Output for Groups

Try This Example

Calculate the minimum, median, and maximum weights for groups of patients and return these results as arrays for each group. splitapply concatenates the output arguments so that you can distinguish output for each group from output for the other groups.

Define a function that returns the minimum, median, and maximum as a row vector.

Load patient weights, genders, and status as smokers from patients.mat.

Use findgroups and splitapply to split the patient weights into groups and calculate statistics for each group.

In this example, you can return nonscalar output as row vectors because the data and grouping variables are column vectors. Each row of Y contains statistics for a different group of patients.

Split Table Data Variables and Apply Function

Try This Example

Calculate the mean body-mass-index (BMI) from tables of patient data. Group the patients by gender and status as smokers or nonsmokers.

Load patient data and grouping variables into tables.

Define a function that calculates mean BMI from the weights and heights of groups or patients.

Create a table that contains the mean BMI for each group.

Return Multiple Statistics for Groups

Try it in MATLAB

Calculate the minimum, mean, and maximum heights for groups of patients and return results in a table.

Define a function in a file named multiStats.m that accepts an input vector and returns the minimum, mean, and maximum values of the vector.

Load patient data into a table.

Group patient heights by gender. Create a table that contains the outputs from multiStats for each group.

Input Arguments

collapse all

func — Function to apply to groups of data
function handle

Function to apply to groups of data, specified as a function handle.

If func returns a nonscalar output argument, then the argument must be oriented so that splitapply can concatenate the output arguments from successive calls to func. For example, if the input data variables are column vectors, then func must return either a scalar or a row vector as an output argument..