You are on page 1of 6

Safety in Delhi Districts

August 30, 2022

Exercise-1
Date: 29 August 2022
Note: Work on this exercise tonight. We will discuss this in the class tomorrow.
A survey was conducted to assess the feeling of safety of adult males and females in different
districts of Delhi. The collected data is enclosed. Note that the data file provides the number of
males and females who feel safe or unsafe in different districts. We want to estimate the effect of
gender and district on feeling of safety.
a. Identify the explanatory and response variables. What are their scales of measurement?
b. Is the response a random variable? If yes, what distribution is it likely to follow? What
assumptions did you make to identify the distribution?
c. State the problem in terms of a statistical model.
d. Find the effect of districts on feeling of safety. Construct a suitable contingency table
(marginal table) and study the effect.
e. Study the effect of gender on feeling of safety using a marginal table.
f. Construct partial tables to verify whether the feeling of safety across districts changes as the
gender changes. In other words, examine whether gender and districts have an interaction
effect on feeling of safety. How will you examine the interaction effect graphically?

[16]: import pandas as pd


import numpy as np
import matplotlib.pyplot as plt

[17]: safety_data = pd.read_excel("D:\Download\Feeling of Safety across Districts and␣


,→Gender.xlsx")

[18]: safety_data

[18]: District Gender Unsafe Safe Sum Unnamed: 5


0 Central Female 4 14 18 NaN NaN
1 Dwarka Female 13 13 26 NaN NaN
2 East Female 8 27 35 NaN NaN
3 New Delhi Female 0 7 7 NaN NaN
4 North Female 7 14 21 NaN NaN
5 North East Female 7 21 28 NaN NaN

1
6 North West Female 14 32 46 NaN NaN
7 Outer Female 9 42 51 NaN NaN
8 Rohini Female 30 25 55 NaN NaN
9 Shahdara Female 4 15 19 NaN NaN
10 South Female 8 29 37 NaN NaN
11 South East Female 17 24 41 NaN NaN
12 South West Female 4 16 20 NaN NaN
13 West Female 12 27 39 NaN NaN
14 Central Male 7 15 22 NaN NaN
15 Dwarka Male 5 22 27 NaN NaN
16 East Male 9 21 30 NaN NaN
17 New Delhi Male 1 1 2 NaN NaN
18 North Male 7 18 25 NaN NaN
19 North East Male 6 34 40 NaN NaN
20 North West Male 10 36 46 NaN NaN
21 Outer Male 9 28 37 NaN NaN
22 Rohini Male 20 23 43 NaN NaN
23 Shahdara Male 7 20 27 NaN NaN
24 South Male 9 27 36 NaN NaN
25 South East Male 8 31 39 NaN NaN
26 South West Male 5 13 18 NaN NaN
27 West Male 11 23 34 NaN NaN

[19]: safety_data.columns

[19]: Index(['District', 'Gender', 'Unsafe', 'Safe', 'Sum', 'Unnamed: 5', ' '],
dtype='object')

[20]: safety_data.drop(['Unnamed: 5', ' '],axis=1,inplace=True)

[21]: safety_data.sort_values(by='District',inplace=True)

[22]: safety_data.reset_index(inplace=True,drop=True)

[8]: safety_data

[8]: District Gender Unsafe Safe Sum


0 Central Female 4 14 18
1 Central Male 7 15 22
2 Dwarka Female 13 13 26
3 Dwarka Male 5 22 27
4 East Female 8 27 35
5 East Male 9 21 30
6 New Delhi Female 0 7 7
7 New Delhi Male 1 1 2
8 North Female 7 14 21
9 North Male 7 18 25

2
10 North East Female 7 21 28
11 North East Male 6 34 40
12 North West Female 14 32 46
13 North West Male 10 36 46
14 Outer Male 9 28 37
15 Outer Female 9 42 51
16 Rohini Female 30 25 55
17 Rohini Male 20 23 43
18 Shahdara Male 7 20 27
19 Shahdara Female 4 15 19
20 South Male 9 27 36
21 South Female 8 29 37
22 South East Male 8 31 39
23 South East Female 17 24 41
24 South West Male 5 13 18
25 South West Female 4 16 20
26 West Female 12 27 39
27 West Male 11 23 34

[26]: safety_data_partial_table = safety_data.copy()

[28]: safety_data_partial_table.insert(5,'Unsafe_percentage','')

[29]: safety_data_partial_table.Unsafe_percentage = ((safety_data_partial_table.


,→Unsafe/safety_data_partial_table.Sum)*100).round(2)

[30]: safety_data_partial_table

[30]: District Gender Unsafe Safe Sum Unsafe_percentage


0 Central Female 4 14 18 22.22
1 Central Male 7 15 22 31.82
2 Dwarka Female 13 13 26 50.00
3 Dwarka Male 5 22 27 18.52
4 East Female 8 27 35 22.86
5 East Male 9 21 30 30.00
6 New Delhi Female 0 7 7 0.00
7 New Delhi Male 1 1 2 50.00
8 North Female 7 14 21 33.33
9 North Male 7 18 25 28.00
10 North East Female 7 21 28 25.00
11 North East Male 6 34 40 15.00
12 North West Female 14 32 46 30.43
13 North West Male 10 36 46 21.74
14 Outer Male 9 28 37 24.32
15 Outer Female 9 42 51 17.65
16 Rohini Female 30 25 55 54.55
17 Rohini Male 20 23 43 46.51

3
18 Shahdara Male 7 20 27 25.93
19 Shahdara Female 4 15 19 21.05
20 South Male 9 27 36 25.00
21 South Female 8 29 37 21.62
22 South East Male 8 31 39 20.51
23 South East Female 17 24 41 41.46
24 South West Male 5 13 18 27.78
25 South West Female 4 16 20 20.00
26 West Female 12 27 39 30.77
27 West Male 11 23 34 32.35

[34]: safety_data_marginal = safety_data.copy()

[35]: safety_data_marginal.drop(['Gender'],axis=1)

[35]: District Unsafe Safe Sum


0 Central 4 14 18
1 Central 7 15 22
2 Dwarka 13 13 26
3 Dwarka 5 22 27
4 East 8 27 35
5 East 9 21 30
6 New Delhi 0 7 7
7 New Delhi 1 1 2
8 North 7 14 21
9 North 7 18 25
10 North East 7 21 28
11 North East 6 34 40
12 North West 14 32 46
13 North West 10 36 46
14 Outer 9 28 37
15 Outer 9 42 51
16 Rohini 30 25 55
17 Rohini 20 23 43
18 Shahdara 7 20 27
19 Shahdara 4 15 19
20 South 9 27 36
21 South 8 29 37
22 South East 8 31 39
23 South East 17 24 41
24 South West 5 13 18
25 South West 4 16 20
26 West 12 27 39
27 West 11 23 34

[32]: from itertools import groupby

4
[42]: safety_data_marginal_table = safety_data_marginal.
,→groupby(['District'],as_index=False).sum()

[47]: safety_data_marginal_table.insert(4,'Unsafe_percentage','')

[48]: safety_data_marginal_table.Unsafe_percentage = ((safety_data_marginal_table.


,→Unsafe/safety_data_marginal_table.Sum)*100).round(2)

[49]: safety_data_marginal_table

[49]: District Unsafe Safe Sum Unsafe_percentage


0 Central 11 29 40 27.50
1 Dwarka 18 35 53 33.96
2 East 17 48 65 26.15
3 New Delhi 1 8 9 11.11
4 North 14 32 46 30.43
5 North East 13 55 68 19.12
6 North West 24 68 92 26.09
7 Outer 18 70 88 20.45
8 Rohini 50 48 98 51.02
9 Shahdara 11 35 46 23.91
10 South 17 56 73 23.29
11 South East 25 55 80 31.25
12 South West 9 29 38 23.68
13 West 23 50 73 31.51

A. Explanatory varibales are(Xi): Gender,District


Response Varibale(y): Feeling of safety
Measurement Scales:
District : Nominal
Gender: Ordinal
Feeling of safety: Ordinal

[50]: safety_data_marginal_table.Unsafe_percentage.plot()

[50]: <AxesSubplot:>

5
[ ]:

You might also like