In: Computer Science
In Python:
A hospital administrator wished to study the relation between patient satisfaction and patient’s age (years), severity of illness (an index), and anxiety level (an index). The administrator randomly selected 46 patients and collected the data, where larger values are associated with more satisfaction, increased severity of illness, and more anxiety.
(a) Plot satisfaction against severity of illness for young patients (less than 40 years old). Describe the relationship briefly: direct or inverse; linear or curved; strong, moderate, or weak.
(b) Plot satisfaction against the anxiety level for both young and old patients. Do you think anxiety predicts patients satisfaction? Explain!
Data:
satis age severity anxiety
48 50 51 2.3
57 36 46 2.3
66 40 48 2.2
70 41 44 1.8
89 28 43 1.8
36 49 54 2.9
46 42 50 2.2
54 45 48 2.4
26 52 62 2.9
77 29 50 2.1
89 29 48 2.4
67 43 53 2.4
47 38 55 2.2
51 34 51 2.3
57 53 54 2.2
66 36 49 2.0
79 33 56 2.5
88 29 46 1.9
60 33 49 2.1
49 55 51 2.4
77 29 52 2.3
52 44 58 2.9
60 43 50 2.3
86 23 41 1.8
43 47 53 2.5
34 55 54 2.5
63 25 49 2.0
72 32 46 2.6
57 32 52 2.4
55 42 51 2.7
59 33 42 2.0
83 36 49 1.8
76 31 47 2.0
47 40 48 2.2
36 53 57 2.8
80 34 49 2.2
82 29 48 2.5
64 30 51 2.4
37 47 60 2.4
42 47 50 2.6
66 43 53 2.3
83 22 51 2.0
37 44 51 2.6
68 45 51 2.2
59 37 53 2.1
92 28 46 1.8
satis = []
age = []
severity = []
anxiety = []
#getting data from the file and storing it in the lists
file = open('data.txt','r')
while True:
# Get next line from file
line = file.readline()
# if line is empty
# end of file is reached
if not line:
break
line = line.rstrip('\n').split()
satis.append(int(line[0]))
age.append(int(line[1]))
severity.append(int(line[2]))
anxiety.append(float(line[3]))
file.close()
#collecting data for young patients(who are less than 40 years of age)
youngSatis = []
oldSatis = []
youngSeverity = []
youngAnxiety = []
oldAnxiety = []
#segregating data based on the age of the patient
for i in range(len(age)):
if age[i]<40:
youngSatis.append(satis[i])
youngSeverity.append(severity[i])
youngAnxiety.append(anxiety[i])
else:
oldSatis.append(satis[i])
oldAnxiety.append(anxiety[i])
#plotting satisfaction against severity for young patients
import matplotlib.pyplot as plt
plt.scatter(youngSatis,youngSeverity,label = 'young patient',color='blue')
plt.title('young patient satisfaction vs severity')
plt.xlabel('Severity')
plt.ylabel('Satisfaction')
plt.legend()
plt.show()
plt.scatter(youngSatis,youngAnxiety,label = 'yount patient',color='blue')
plt.scatter(oldSatis,oldAnxiety,label = 'old patient',color= 'red')
plt.xlabel('Satisfaction')
plt.ylabel('Anxiety')
plt.legend()
plt.show()
Code Screenshot:
Output:
a)
We can see from the scatter plot that as the satisfaction levels drops, the severity is increasing for most patients. There are outliers present, but the general trend of the graph shows us that satisfaction and severity are inversely related. The relationship between the two is a curved due to many outliers that are present. Based on the graph, we can say that the 2 parameters have a moderate realationship.
b)
Yes, we can confidently say that anxiety predits the satisfaction levels. The two variables have a linear realationship between them as can be seen from the graph plotted. The old patients have got higher anxiety and lower satisfaction levels while the young patients have lower anxiety and much higher satisfaction levels.