-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcensus income.Rmd
More file actions
2552 lines (1698 loc) · 134 KB
/
Copy pathcensus income.Rmd
File metadata and controls
2552 lines (1698 loc) · 134 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#PROJECT ON CENSUS INCOME DATASET
<h1 id="top11">Table of Contents</h1>
<ol type="1">
<li><a href="#top12">Objective</a></li>
<li><a href="#top">Exploratory Data Analysis</a></li>
<ol type="a">
<li><a href="#top6">Summary of Dataset</a></li>
<li><a href="#top7">Cleaning Data</a></li>
<li><a href="#top8">Univariate Distribution</a></li>
<ul style="list-style-type:square;"><li><a href="#top13">Conclusion-Univariate Distribution</a></li></ul>
<li><a href="#top9">Bivariate Distribution</a></li>
<ul style="list-style-type:square;"><li><a href="#top14">Conclusion-Bivariate Distribution</a></li></ul>
<li><a href="#top10">Multivariate Distibution</a></li>
<ul style="list-style-type:square;"><li><a href="#top15">Conclusion-Multivariate Distribution</a></li></ul>
</ol>
<li><a href="#top1">Predictive Modeling</a></li>
<ul style="list-style-type:disc;">
<li><a href="#top2">Logistic Regression Model</a></li>
<li><a href="#top3">Decision Tree Model</a></li>
<li><a href="#top4">Random Forest Model</a></li>
</ul>
<li><a href="#top5">Conclusion</a></li>
</ol>
<h1 id="top12"><u>Objective</u></h1>
In this project, we will use the US census data to build a model to predict if the income of any individual in the US is greater than or less than USD 50000 based on the information available about that individual in the census data.
The dataset used for the analysis is an extraction from the 1994 census data by Barry Becker and donated to the public site http://archive.ics.uci.edu/ml/datasets/Census+Income. This dataset is popularly called the "Adult" data set.
We will use different visualization techniques to conduct a preliminary analysis of the impact of different variables on each other and finally we will predict whether a person makes over 50K a year.
<h1 id="top"><u>Exploratory Data Analysis</u></h1>
###Reading the Dataset
```{r}
adult <- read.csv("C:/Users/u22v05/Desktop/dataset/adult.txt", header=FALSE)
adult
adult <- setNames(adult,c("age","Workclass","fnlwgt","education","educationnum","maritalstatus","occupation","relationship","race","sex","capitalgain","capitalloss","hoursperweek","nativecountry","salary")) #changing column names for convinience
library(DataExplorer)
```
###Loading necessary packages for this analysis:
```{r}
library(ggplot2)
library(plyr)
library(dplyr)
library(tidyr)
library(corrplot)
library(descr)
library(knitr)
```
###Data Description
```{r}
dim(adult)[1]
```
<ul style="list-style-type:disc;"><li>This indicates that the total number of records in the file "adult.data" is 32561.</li></ul>
```{r}
dim(adult)[2]
```
<ul style="list-style-type:disc;"><li>
Originally in the adult data set there are 14 attributes but here we have included 15th attribute which is salary which describes whether the salary is >50k or not.</li></ul>
```{r}
str(adult)
```
###Input variables in the dataset are:
```{r echo=FALSE, message=FALSE, warning=FALSE, paged.print=FALSE}
output <-
matrix(paste(c("Age","Age of the individual","Continuous","Numeric","Workclass","Class of Work","Categorical","Private, Self-emp-not-inc, Self-emp-inc, Federal-gov, Local-gov, State-gov, Without-pay, Never-worked","fnlwgt","Final Weight Determined by Census Org","Continuous","Numeric","Education","Education of the individual","Ordered Factor","Bachelors, Some-college, 11th, HS-grad, Prof-school, Assoc-acdm, Assoc-voc, 9th, 7th-8th, 12th, Masters, 1st-4th, 10th, Doctorate, 5th-6th, Preschool","Education-num","Number of years of education","Continuous","Numeric","Marital-status","Marital status of the individual","Categorical","Married-civ-spouse, Divorced, Never-married, Separated, Widowed, Married-spouse-absent, Married-AF-spouse","Occupation","Occupation of the individual","Categorical","Tech-support, Craft-repair, Other-service, Sales, Exec-managerial, Prof-specialty, Handlers-cleaners, Machine-op-inspct, Adm-clerical, Farming-fishing, Transport-moving, Priv-house-serv, Protective-serv, Armed-Forces","Relationship","Present relationship","Categorical","Wife, Own-child, Husband, Not-in-family, Other-relative, Unmarried","Race","Race of the individual","Categorical","White, Asian-Pac-Islander, Amer-Indian-Eskimo, Other, Black","Sex","Sex of the individual","Categorical","Female, Male","Capital-gain","Capital gain made by the individual","Continuous","Numeric","Capital-loss","Capital loss made by the individual","Continuous","Numeric","Hours-per-week","Average number of hours spent by the individual on work","Continuous","Numeric","Native-country","Average number of hours spent by the individual on work","Categorical","United-States, Cambodia, England, Puerto-Rico, Canada, Germany, Outlying-US(Guam-USVI-etc), India, Japan, Greece, South, China, Cuba, Iran, Honduras, Philippines, Italy, Poland, Jamaica, Vietnam, Mexico, Portugal, Ireland, France, Dominican-Republic, Laos, Ecuador, Taiwan, Haiti, Columbia, Hungary, Guatemala, Nicaragua, Scotland, Thailand, Yugoslavia, El-Salvador, Trinadad&Tobago, Peru, Hong, Holand-Netherlands")),
ncol=4, byrow = TRUE)
library(htmlTable)
htmlTable( output, header = paste(c("Variable Name","Description","Type","Possible Values")), css.cell = rbind(rep("background: lightblue;", times=ncol(output)),matrix("", ncol=ncol(output), nrow=nrow(output))), col.rgroup = c("none", "light grey"))
```
####Detailed Description of variables:
<ol type="1">
<li> <b>age:</b>the age of an individual- Continuous.(numeric)</li>
<li> <b>workclass:</b> a term to represent the employment status of an individual:Private, Self-emp-not-inc(Self employed not incorporated-Refers to people who work for themselves in corporate entities.), Self-emp-inc(Self employed incorporated-Refers to people who work for themselves in corporate entities.), Federal-gov(Federal Government), Local-gov(local government), State-gov(State Government), Without-pay, Never-worked.(Categorical)</li>
<li><b>fnlwgt(final weight):</b>In other words, this is the number of people the census believes the entry represents..-Continuous.(numeric)</li>
<li><b>education: </b>The highest level of education achieved by an individual:Bachelors, Some-college, 11th, HS-grad(High School Graduate), Prof-school(Professional school), Assoc-acdm(Associates degree-academic program), Assoc-voc(Associates degree-vocational), 9th, 7th-8th, 12th, Masters, 1st-4th, 10th, Doctorate, 5th-6th, Preschool.(Categorical)</li>
<li><b>education-num:</b>Number of years of education in total,which is a continuous representation of the variable education(Continuous) </li>
<li><b>maritalstatus:</b>Marital status of an individual. Married-civ-spouse corresponds to a civilian spouse while Married-AF-spouse is a spouse in the Armed Forces.- Married-civ-spouse, Divorced, Never-married, Separated, Widowed, Married-spouse-absent, Married-AF-spouse.(Categorical)</li>
<li><b>occupation:</b> the general type of occupation of an individual: Tech-support(Techinical support), Craft-repair(Does not require high education,just repairing works), Other-service, Sales, Exec-managerial(Executive manager), Prof-specialty(Speciality in a particular profession), Handlers-cleaners, Machine-op-inspct(Machine operator inspector), Adm-clerical(Admin Clerical), Farming-fishing, Transport-moving, Priv-house-serv(Private house servant), Protective-serv(Protective Servant), Armed-Forces(Working in army).(Categorical)</li>
<li><b>relationship:</b>represents what this individual is relative to others- Wife, Own-child, Husband, Not-in-family, Other-relative, Unmarried.(Categorical)</li>
<li><b>race:</b>Descriptions of an individual's race- White, Asian-Pac-Islander, Amer-Indian-Eskimo, Other, Black.(Categorical)</li>
<li><b>sex: </b>the biological sex of the individual- Female, Male.(Categorical)</li>
<li><b>capitalgain:</b>(income from investment sources other than salary) Continuous.(numeric)</li>
<li><b>capitalloss:</b>(income from investment sources other than salary) Continuous.(numeric)</li>
<li><b>hoursperweek:</b>(Total number of hours a person work in week ) Continuous.(numeric)</li>
<li><b>nativecountry:</b>Country of origin of the participant: United-States, Cambodia, England, Puerto-Rico, Canada, Germany, Outlying-US(Guam-USVI-etc), India, Japan, Greece, South, China, Cuba, Iran, Honduras, Philippines, Italy, Poland, Jamaica, Vietnam, Mexico, Portugal, Ireland, France, Dominican-Republic, Laos, Ecuador, Taiwan, Haiti, Columbia, Hungary, Guatemala, Nicaragua, Scotland, Thailand, Yugoslavia, El-Salvador, Trinadad&Tobago, Peru, Hong, Holand-Netherlands.(Categorical)</li>
<li> <b>salary:</b>whether salary is >50K or <=50K per year?- >50K ,<=50K(Categorical)</li>
</ol>
<h2 id="top6">Summary of Dataset</h2>
```{r}
summary(adult) #summary of dataset
```
####Summary function generates the summary statistics of each variable.There are total 15 attributes out of which 8 are categorical and 7 are numerical.
<ol type="1">
<li>Age</li>
<ul style="list-style-type:square;">
<li>The summary statisitcs of age tells us that mean age is 38.5 years and median age is 37 years which shows that maximum participants are of working age .Mean is greater median shows that the graph will be right skewed.</li>
<li>Minimum age of a participant is 17 years whereas maximum age is 90 years ,these two are extreme values.This indicates that there are few participants who are earning either by working or through capital gain even at he age 17 and 90 years.</li>
<li>25% of the participants comes under the age of 28.</li>
<li>50 % of the participants comes under the age of 37 which again indicates that mostly young people who are working is being considered.</li>
<li>75% of the participants comes under the age of 48 years shows that majority people who are working and are in their 30s and 40s are being considered.</li>
</ul>
<li>Workclass</li>
<ul style="list-style-type:square;">
<li>The summary statisitcs of Workclass shows that maximum number of participants are in "Private" job i.e. 22696.</li>
<li>The number of participants in "Self-employed-not incorporated" category are 2541.</li>
<li>The number of participants in "Local government" are 2093.</li>
<li>1836 values are missing.</li>
<li>The number of participants in "State government" category are 1298. </li>
<li>The number of participants in "Self-employed-not incorporated" category are 1116.</li>
<li>The number of participants in "Self-employed incorporated" category are 981.</li>
<li>This statistics shows that more than 50% of the participants are in "Private " job means majority of people are working in private sector.</li>
</ul>
<li>Final Weight:</li>
<ul style="list-style-type:square;">
<li>Minimum number of 12285 people of population have similar characteristic to a particular record of adult dataset.</li>
<li>Maximum number of 1484705 people of population have similar characteristic to a particular record of adult dataset.</li>
<li>117827 indicates the 25% of total population of US in 1994.</li>
<li>178356 indicates the 50% of total population of US in 1994.</li>
<li>237051 indicates the 75% of total population of US in 1994.</li>
<li>On an avegare 189778 people have similar characteristic to a particular record of adult dataset.
<li>The total sum of the final weight column gives the whole population of US in 1994.</li>
</ul>
<li>Education:</li>
<ul style="list-style-type:square;">
<li>1175 participants are of 11th grade.</li>
<li>Maximum participants are High school graduate i.e. 10501.</li>
<li>7291 participants are of some college but they don't have a degree.</li>
<li>1382 participants holds Associates degree(vocational)</li>
<li>5355 participants hold Bachelors degree(Example-BA AB BS)</li>
<li>1723 participants holds Masters degree(Like MA MS MEng MEd MSW MBA)</li>
<li>5134 participants are of other categories.</li>
<li>Again division is non homogeneous because maximum participants are high school graduate.</li>
</ul>
<li>Education Number</li>
<ul style="list-style-type:square;">
<li>Minimum number of years for which a participant has studied is 1 year whereas the maximum number of years of education is 16 years.</li>
<li>25% of the population has studied for 9 years.</li>
<li>75% of the population has studied for 12 years.</li>
<li>Mean is 10.08 years whereas median is 10.00 years which shows that thereia almost symmetrical distribution of the data.</li>
</ul>
<li>Marital-Status</li>
<ul style="list-style-type:square;">
<li>Maximum number of participants are Married to civilian spouse i.e. 14976 which is about half of the participants.</li>
<li>10683 participants never got married which is second highest.
<li>4443 people are divorced.</li>
<li>1025 people are separated from their spouse.
<li>993 people are widowed.</li>
<li>418 participant's spouse is absent.</li>
<li>Least number of participants are married to Army Force Spouse.</li>
<li> we can see the non homogeneous distribution of data.</li>
</ul>
<li>Occupation</li>
<ul style="list-style-type:square;">
<li>The occupation statistics shows us the frequency of participants indulged in different occupation.The division is almost balanced which can be observed from the below data.</li>
<li>Participants of occupation "Admin support including clerical" is 3770.</li>
<li>Participants of occupation Professional specialty is 4140.</li>
<li>Participants of occupation Executive admin and managerial 4066.</li>
<li>Participants who are in Other servicea are 3295.</li>
<li>Participants indulged in Sales are 3650.</li>
<li>Participants of occupation "Precision production craft & repair" is 4099.</li>
<li>Participants indulged in other occupation is 9541.</li>
</ul>
<li>Relationship</li>
<ul style="list-style-type:square;">
<li>The summary statistics of relationship shows the frequency of participants having a particular relationship with others in family.</li>
<li>Participants who are Husband is 13193 which is maximum in number .It can be seen in reality mostly male members are working in a family.</li>
<li>8305 people dont have a family which is second highest.</li>
<li>Participants who are wives are 1568.</li>
<li>3446 people are unmarried.</li>
<li>5068 are own-child.(Basically these are the people who are single,they maybe married or not.)
<li>981 people are the "other relatives" of other people.</li>
</ul>
<li>Race</li>
<ul style="list-style-type:square;">
<li>The summary statistics of race tells us the number of participants belonging to a particular race.There is unbalanced distribution because more than 50% of the participant are of white race.</li>
<li>White participants are maximum i.e. 27816 as most of the people living in US are of white race.</li>
<li>Black people are 3124.</li>
<li>Asian or Pacific Islander participants are 1039.</li>
<li>Participants of Other race are 3124.</li>
<li>Amer Indian Eskimo 311 which is least in number.</li>
</ul>
<li>Sex</li>
<ul style="list-style-type:square;">
<li>Summary statistics of sex tells us about the number of male and female participants.</li>
<li>Male participants are 21790.</li>
<li> participants are 10771.</li>
<li>Male participants are double of females as in reality working males are higher than females.The distribution of data is unbalanced.</li>
</ul>
<li>Capital Gain</li>
<ul style="list-style-type:square;">
<li>Summary statistics of Capital Gain tells us about the gain of a participant from other sources apart from working.</li>
<li>Maximum Capital Gain is 99999 whereas minimum capital gain is 0.</li>
<li>It is observed that 75% of the population has 0 capital gain.</li>
<li>We get the mean capital gain as 1078 whereas median 0 means data is highly right skewed.</li>
</ul>
<li>Capital Loss</li>
<ul style="list-style-type:square;">
<li>Summary statistics of Capital loss tells us about the loss of participants from other sources apart from working.</li>
<li>Maximum Capital loss is 4356 whereas minimum capital loss is 0.</li>
<li>It is observed that 75% of the population has 0 capital loss.</li>
<li>We get the mean capital loss as 87 whereas median is 0 which means data is right skewed.</li>
</ul>
<li>Hours Per week</li>
<ul style="list-style-type:square;">
<li>Minimum working hour per week is 1 hour.</li>
<li>Maximum working hours per week is 99 hours.</li>
<li>Mean working hours of a week for 25% of poppulation is 40 hours.</li>
<li> working hours of a week for 75% of poppulation is 45 hours.</li>
<li>Mean working hours of a week for 50% of the population is 40.44 hours where as median is 40 hours. Mean and median are almost equal which shows the symmetrical distribution of data..</li>
</ul>
<li>Native Country</li>
<ul style="list-style-type:square;">
<li>The summary statistics of native country tells us the number of participants belonging to a particular country originally.</li>
<li>29170 participants are originally from United States.</li>
<li>643 participants are originally from Mexico.</li>
<li>198 participants are originally from Philippines.</li>
<li>137 participants are originally from Germany.</li>
<li>121 participants are originally from Canada.</li>
<li>1709 participants are originally from Other countries.</li>
<li>Data is highly unbalanced as maximum participants are from US.</li>
</ul>
<li>Salary</li>
<ul style="list-style-type:square;">
<li>24720 people have salary <=50k whereas 7841 people have salary >50k.This shows the unbalanced distribution of data.</li>
</ul>
</ol>
<h2 id="top7">CLEANING DATA</h2>
```{r}
library(tidyr)
#There are some missing valus in given dataset so we are replacing these missing values with NAs
idx <- adult == " ?" #finding out '?' in dataset and replacing them with NA
is.na(adult) <- idx
colSums(is.na(adult)) #finding out number of NA
plot_missing(adult)
```
<ul style="list-style-type:square;">
<li>From the above table we can see that there are 1836 missing values in Workclass variable ,1843 missing values in Occupation variable and 583 missing values in Native country column.</li>
<li>Here we have NAs present in 3 categorical variables.It is neccessary to treat the missing values because some classification algorithm will fail if they are passed with data containing missing values.</li>
<li>Treating NAs :We can treat NAs in the following ways.</li>
<ol type="1">
<li>DROPPING NULL OR MISSING VALUES-This is the fastest and easiest step to handle missing values. However, it is not generally advised. This method reduces the quality of our model as it reduces sample size because it works by deleting all other observations where any of the variable is missing.</li>
<li>FILL MISSING VALUES WITH TEST STATISTIC-This is the most common method of handling missing values. This is a process whereby missing values are replaced with a test statistic like mean, median or mode of the particular feature the missing value belongs to.For categorical variables,we can replace missing values by frequently occuring value in that column.</li>
<li>PREDICTIVE MODEL FOR HANDLING MISSING DATA-Depending on the class of data that is missing, one can either use a regression model or classification to predict missing data. This works by turning missing features to labels themselves and now using columns without missing values to predict columns with missing values.</li>
</ol>
</ul>
```{r}
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
adult <- lapply(adult, function(x) {
if(is.factor(x)) replace(x, is.na(x), Mode(na.omit(x)))
else if(is.numeric(x)) replace(x, is.na(x), mean(x, na.rm=TRUE))
else x
})
adult <- as.data.frame(adult)
colSums(is.na(adult))
```
<ul style="list-style-type:square;"><li>
Replaced missing values with frequently occuring value of that variable not with mean value because NAs are present in categorical variable and we cannot find it's mean and omiting NAs will reduce size of data.</li>
</ul>
<h2 id="top8">Univariate Distribution</h2>
###Analysis of variable "Salary"
```{r}
library(scales)
ggplot(adult,aes(x = adult$salary, fill = adult$salary)) +geom_bar(aes(y = (..count..)/sum(..count..))) +
geom_text(aes(label = scales::percent((..count..)/sum(..count..)),y = (..count..)/sum(..count..) ), stat = "count", vjust = -.1,size=5) +labs(x = "Salary", y = "",fill = "Salary") +theme(legend.position = 'none',axis.text.x = element_text( hjust = 1)) + scale_y_continuous(labels = percent)
```
<ul style="list-style-type:disc;"><li>
The graph above shows us that 75.9% of people earn less than or equal to 50k means the data is unbalanced and we may get biased results that salary is usually less than or equal to 50k.</li>
</ul>
###Analysis of variable "Age"
```{r}
ggplot(data =adult, aes(age)) +
geom_density(alpha = 0.2,fill="pink") +
scale_x_continuous(breaks = seq(0, 95, 5))
```
<ul style="list-style-type:disc;"><li>
Above graph shows that majority of working population lies in the age group of 20 to 50 years this situation is reasonable in reality beacause working age of people lies in this range only.As the age increases after 37 years there is decrease in working class people.There are some outliers, such as individuals who are 77 years or above are also earning either by capital gain or by working. </li>
</ul>
```{r}
aduage <- select(adult,age)
aduage2 <- filter(aduage,age>27 & age<47)
count(aduage2)
```
<ul style="list-style-type:disc;"><li>
Above count clearly shows us maximum participants are of age 27 to 47 years.</li>
</ul>
###Analysis of variable "Work class"
```{r}
ggplot(adult,aes(x = adult$Workclass, fill = adult$Workclass)) +geom_bar(aes(y = (..count..)/sum(..count..))) +
geom_text(aes(label = scales::percent((..count..)/sum(..count..)),y = (..count..)/sum(..count..) ), stat = "count", vjust = -.1,size=4) +labs(x = "Workclass", y = "",fill = "Workclass") +theme(legend.position = 'none',axis.text.x = element_text( hjust = 1, angle=90)) + scale_y_continuous(labels = percent)+ggtitle("Bar Plot of Work Class")
```
<ul style="list-style-type:disc;"><li>
The above bar plot of the Work Class indicates that we will find maximum participants that is 75.3% people are working in the private job and there is no participant who never worked.</li>
</ul>
```{r}
table(adult$Workclass)
```
<ul style="list-style-type:disc;">
<li>Maximum people work in private jobs that is 24532</li>
<li>2541 people are "self employed not incorporated" which is second highest after the people in private jobs </li>
<li> The number of participants in "State government" category are 1298 </li>
<li> The number of participants in "Self-employed-not incorporated" category are 1116</li>
<li> The number of participants in "Self-employed incorporated" category are 960</li>
<li>The difference between in private job and people working in any other category is very huge which shows majority of participants works in private job.</li>
</ul>
###Analysis of variable "Education"
```{r}
ggplot(adult,aes(x = adult$education, fill = adult$education)) +geom_bar(aes(y = (..count..)/sum(..count..))) +geom_text(aes(label = scales::percent((..count..)/sum(..count..)),y = (..count..)/sum(..count..) ), stat = "count", vjust = -.1,size=4) +labs(x = "Education", y = "",fill = "Education") +theme(legend.position = 'none',axis.text.x = element_text( hjust = 1, angle=90)) + scale_y_continuous(labels = percent)+ggtitle("Bar Plot of Education")
```
<ul style="list-style-type:disc;">
<li>The above barplot of Education gives the inference that the maximum number of people are High School Graduate and minimum number of participant belongs to 1st-4th class and preschool which is relatable situation because during school usually kids don't work and after graduating from high school, students start living on their own in US.</li>
</ul>
```{r}
table(adult$education)
```
<ul style="list-style-type:disc;">
<li>1175 participants are of 11th grade.</li>
<li>Maximum participants are High school graduate i.e. 10501.</li>
<li>7291 participants are of some college but they don't have a degree.</li>
<li>1382 participants holds Associates degree(vocational)</li>
<li>5355 participants hold Bachelors degree(Example-BA AB BS)</li>
<li>1723 participants holds Masters degree(Like MA MS MEng MEd MSW MBA)</li>
</ul>
###Analysis of variable "Marital Status"
```{r}
ggplot(adult,aes(x = adult$maritalstatus, fill = adult$maritalstatus)) +geom_bar(aes(y = (..count..)/sum(..count..))) +
geom_text(aes(label = scales::percent((..count..)/sum(..count..)),y = (..count..)/sum(..count..) ), stat = "count", vjust = -.1,size=4) +labs(x = "Marital Status", y = "",fill = "Marital Status") +theme(legend.position = 'none',axis.text.x = element_text( hjust = 1, angle=90)) + scale_y_continuous(labels = percent)+ggtitle("Bar Plot of Marital Status")
```
<ul style="list-style-type:disc;"><li>
A person married to a civilian spouse is more likely to work as civilians are more educated and people who never married are more as these are college going or High School graduate students who were large in number.</li>
</ul>
```{r}
table(adult$maritalstatus)
```
<ul style="list-style-type:disc;">
<li>The count of people married to civilian spouse is 14976 which clearly indicates that most of the working class people are married and educated.</li>
<li>10683 people who never married are second highest in number these are college going or High School graduate students who were large in number.</li>
<li>4443 people are divorced.</li>
<li>1025 people are separated from their spouse.</li>
<li>993 people are widowed</li>
<li>418 participant's spouse is absent.</li>
</ul>
###Analysis of variable "Occupation"
```{r}
ggplot(adult,aes(x = adult$occupation, fill = adult$occupation)) +geom_bar(aes(y = (..count..)/sum(..count..))) +
geom_text(aes(label = scales::percent((..count..)/sum(..count..)),y = (..count..)/sum(..count..) ), stat = "count", vjust = -.1,size=4) +labs(x = "Occupation", y = "",fill = "Occupation") +theme(legend.position = 'none',axis.text.x = element_text( hjust = 1, angle=90)) + scale_y_continuous(labels = percent)+ggtitle("Bar Plot of Occupation")
```
<ul style="list-style-type:disc;"><li>
The above graph indicates that a person who has speciality in particular profession is more in number.There is large number of people even from "craft repair" which does not require much education. Population is evenly distributed among different occupation.</li>
</ul>
```{r}
table(adult$occupation)
```
<ul style="list-style-type:disc;">
<li>Maximum people have professional speciality that is 5983.</li>
<li>Participants of occupation "Admin support including clerical" is 3770</li>
<li>Participants of occupation Executive admin and managerial 4066</li>
<li>Participants who are in Other services are 3295</li>
<li>Participants indulged in Sales are 3650</li>
<li>Participants of occupation "Precision production craft & repair" is 4099</li>
</ul>
###Analysis of variable "Relationship"
```{r}
ggplot(adult,aes(x = adult$relationship, fill = adult$relationship)) +geom_bar(aes(y = (..count..)/sum(..count..))) +
geom_text(aes(label = scales::percent((..count..)/sum(..count..)),y = (..count..)/sum(..count..) ), stat = "count", vjust = -.1,size=4) +labs(x = "Relationship", y = "",fill = "Relationship") +theme(legend.position = 'none',axis.text.x = element_text( hjust = 1, angle=90)) + scale_y_continuous(labels = percent)+ggtitle("Bar Plot of Relationship")
```
<ul style="list-style-type:disc;"><li>
Above graph shows that maximum participants are husbands which is reasonable as male's working percentage is higher and usually male member has the responsibility.</li></ul>
```{r}
table(adult$relationship)
```
<ul style="list-style-type:disc;">
<li>Majority of people are husbands that is 13193 as they are male and have responsibility of family.</li>
<li>8305 people dont have a family which is second highest.</li>
<li>Participants who are wives are 1568.</li>
<li>3446 people are unmarried.</li>
<li>5068 are own-child.(Basically these are the people who are single,they maybe married or not.)</li>
<li>981 people are the "other relatives" of other people.</li>
</ul>
###Analysis of variable "Race"
```{r}
ggplot(adult,aes(x = adult$race, fill = adult$race)) +geom_bar(aes(y = (..count..)/sum(..count..))) +
geom_text(aes(label = scales::percent((..count..)/sum(..count..)),y = (..count..)/sum(..count..) ), stat = "count", vjust = -.1,size=4) +labs(x = "Race", y = "",fill = "Race") +theme(legend.position = 'none',axis.text.x = element_text( hjust = 1)) + scale_y_continuous(labels = percent)+ggtitle("Bar Plot of Race")
```
<ul style="list-style-type:disc;"><li>
The major part of individuals belong to the category "White", followed by the category "Black" . As we can see from the graph above, 85.4% of the participants in the study are "White" because Americans are usually of white race.</li></ul>
```{r}
table(adult$race)
```
<ul style="list-style-type:disc;"><li>
27816 people out of 32561 are of white race which claerly indicated maximum people are from US and of white race.
</li>
<li>3124 people are black which is second highest in number.</li></ul>
###Analysis of variable "Sex"
```{r}
ggplot(adult,aes(x = adult$sex, fill = adult$sex)) +geom_bar(aes(y = (..count..)/sum(..count..))) +
geom_text(aes(label = scales::percent((..count..)/sum(..count..)),y = (..count..)/sum(..count..) ), stat = "count", vjust = -.1,size=4) +labs(x = "Gender", y = "",fill = "Gender") +theme(legend.position = 'none',axis.text.x = element_text( hjust = 1)) + scale_y_continuous(labels = percent)+ggtitle("Bar Plot of Gender")
```
<ul style="list-style-type:disc;"><li>
66.9% of people are male which is quite reasonable because in reality working male percentage is higher than female. </li>
</ul>
```{r}
table(adult$sex)
```
<ul style="list-style-type:disc;"><li>
It is clearly visible mostly working people are male that is 21790 whereas women don't work much as they are only 10771 in number.</li></ul>
###Analysis of variable "capital_gain"
```{r}
ggplot(adult, mapping = aes(x = capitalgain)) + geom_histogram(binwidth = 4000,color = "black",fill = "lightgreen", alpha = 0.8) +ggtitle("Histogram of Capital_Gain")
```
<ul style="list-style-type:disc;"><li>
Capital gain of most people is zero means they do not get extra profit by selling assets.Very few people make large profit by selling assets.</li></ul>
###Analysis of variable "capital_loss"
```{r}
ggplot(adult, mapping = aes(x = capitalloss)) + geom_histogram(binwidth = 1000,color = "black",fill = "lightblue", alpha = 0.8) +ggtitle("Histogram of Capital_loss ")
```
<ul style="list-style-type:disc;"><li>
Capital loss of most people is zero means they don't have extra loss by selling assets.Very few people had extra loss by selling assets.</li>
</ul>
###Analysis of variable "hours_per_week"
```{r}
ggplot(adult, mapping = aes(x = hoursperweek)) + geom_histogram(binwidth = 15,color = "black",fill = "lightpink", alpha = 0.8) +ggtitle("Histogram of Hours_per_week ")
```
<ul style="list-style-type:disc;">
<li>
Most of the people works for 40hours a week which is in accordance with reality as people work for 8 hours in 5 days a week.Also there are people who work for very few hours but still earn and there are people who work for more than 50 hours as well. </li></ul>
```{r}
summary(adult$hoursperweek)
```
<ul style="list-style-type:disc;"><li>On an average peopele work for 40.44 hours per week.
</li>
<li>Minimum working hour per week is 1. </li>
<li> Maximum working per week is 99 hours.</li>
<li>25% of people work for 40 hours as the first quadrant and median are same.</li>
<li> 75% of the population works for 45 hours or less.</li>
</ul>
###Analysis of variable Native country
```{r}
ggplot(adult,aes(x = adult$nativecountry, fill = adult$nativecountry)) +geom_bar(aes(y=(..count..)/sum(..count..))) + geom_text(aes(label = scales::percent((..count..)/sum(..count..)), y = (..count..)/sum(..count..) ), stat = "count", vjust = -.1,size=3,angle=90) +labs(x = "Region", y = "",fill = "Regions") +theme(legend.position = 'none',axis.text.x = element_text(angle = 90, hjust = 1)) + scale_y_continuous(labels = percent)
```
<ul style="list-style-type:disc;"><li>
The majority of people come from the United States that is 91.4% which is likely to happen as this survey is conducted in US only.</li>
</ul>
```{r}
table(adult$nativecountry)
```
<ul style="list-style-type:disc;"><li>Native country of maximum poeple is US.
</li></ul>
<h2 id="top13">Conclusion from Analysis of Univariate Distribution</h2>
<ul style="list-style-type:disc;">
<li>Majority of people earn less than 50k.</li>
<li>Majority of population belongs to age group of 25 to 55 years.</li>
<li> Majority of participants are married male whereas female are very less in number.</li>
<li>Maximum participants are working in private job and are High school graduate.</li>
<li>On an average people work for 40 hours in a week that is 8 hours in 5 days. </li>
<li>Majority of population is of white race are from US.</li>
<li>Very few people gained through capital gain or had loss due to capital loss.Thus capital gain and capital loss doesnot contribute much to the earning of majority of people.
</li></ul>
###Correlation between numerical variable
```{r}
adult$salary <- as.numeric(adult$salary)-1 #Changing salary to 0, 1
#Correlation plot
var <- c(1, 3, 5, 11:13, 15)
corrplot(cor(adult[,var]))
cor(adult[,var])
adult$salary <- factor(adult$salary, labels=c("<=50k", ">50k")) #Re-factoring salary
```
<ul style="list-style-type:disc;">
<li>We can see that the numerical variables do not seem to be strongly correlated with each other. We see that the final weight variable does not appear to have an association with the response either.So we will not consider final weight.</li>
<li>There is low positive correlation between age and salary means as age increases salary also increases.</li>
<li>There is low positive correlation between education number and salary means as education number increases salary also increases , if a person has studied for more years earns more.</li>
<li>Capital gain and salary are slightly correlated as salary increases capital gain increases because people with more salary are likely to invest more.</li>
<li>Capital loss and salary are slightly correlated as salary increases capital loss may increases because people with more salary are likely to invest more so there maybe some chances of loss.</li>
<li>There is low positive correlation between hours per week and salary means as hours per week increases salary also increases , if a person works for more hours earns more.</li>
</ul>
<li><a href="#top11">Go to top:Table of Contents</a></li>
<h2 id="top9">Bivariate Distribution</h2>
###Analysis of age and salary
```{r}
ggplot(data =adult, aes(age, fill = salary)) +
geom_density(alpha = 0.2) +
scale_x_continuous(breaks = seq(0, 95, 5))
```
<ul style="list-style-type:disc;"><li>The above density plot shows us that during the age of 20 to 35 years more people earn <50k whereas in the interval of 37 to 55 years more people earn >50k and then people earning >50k starts decreasing as they move towards the age of retirement.</li>
</ul>
```{r}
ggplot(aes(x = salary, y = age),data = adult) + geom_boxplot() + stat_summary(fun.y = mean, geom = "point",shape = 16,cex = 2,col = "blue") +coord_cartesian(ylim = c(10,90))+scale_y_continuous(breaks = seq(10, 90, 5)) + ylab("Age") +xlab("Salary") +ggtitle("Box Plot of Age by Salary")
```
<ul style="list-style-type:disc;"><li>Older people tends to earn more than younger people. We can see that people who earn more that 50K a year are on average about 42-43 years old, whereas people who earn less than 50K a year are younger, on average around 34-37 years.This leads to the assumption that experience surely matters to earn more.Some people who earn greater than 50k belongs to the older age group that is above 72 years.Similary there are few people who are above 77 years of age earn less than equal to 50k per year.</li></ul>
```{r}
library(ggplot2)
summary(subset(adult$age,adult$salary== "<=50k"))
```
```{r}
summary(subset(adult$age,adult$salary== ">50k"))
```
<ul style="list-style-type:disc;"><li>From the above summary we notice that the first quartiles (25-th percentiles) for both groups differ significantly. The 25% of people who have an income of more than 50K is equal to 36, whereas the 25% of people earning less than 50K equals 25. This means that the older an individual is, the bigger their chances of having a higher income are because income increases with experience.</li>
</ul>
### t test
<ul style="list-style-type:disc;">
<li>Alternative Hypothesis- The average age of people is different in the >50K and <50K samples</li>
<li>Null Hypothesis- The average age of people is same in both >50K and <50K samples
</li></ul>
```{r}
gr <- subset(adult$age,adult$salary== ">50k")
les <- subset(adult$age,adult$salary=="<=50k")
t.test(gr,les,paired=FALSE)
```
<ul style="list-style-type:disc;"><li>Conclusion-Alternative Hypothesis is true and we reject the null hypothesis means average age of people earning more than 50k is different from average age of people earning less than 50k.This shows different age group has different earnings means we will consider age as one of the features during training so it will help in prediction of salary.</li>
</ul>
###Analysis of Workclass and salary
```{r}
ggplot(adult, aes(x= Workclass, group=salary)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop..),
y= ..prop.. ), stat= "count", vjust = -.5,size=3) +
labs(y = "Percent", fill="Workclass") +
facet_grid(~salary) +
scale_y_continuous(labels = scales::percent)+theme(legend.position = 'none',axis.text.x = element_text( hjust = 1,angle=90))+ggtitle("Bar Plot of Salary grouped by workclass")
```
<ul style="list-style-type:disc;"><li>From the barplot above, we can see that the percentage of individuals having an income of less than 50K is biggest for Private jobs that is 78.4% . The group with the second highest percentage of people earning less than 50K is that of category Self employed not incorporated. In the private sector only about 65.7% of the people earn more than 50K a year.People who don't work doesnot earns.These results demonstrate that there is a relationship between the variables "income" and "workclass".</li></ul>
```{r}
# Number of people from different "workclass" earning less than 50K and more than 50K:
table(adult$Workclass,adult$salary)
```
###Test for Independence of Work class and salary.
<ul style="list-style-type:disc;"><li>Null Hypothesis- Workclass and salary are independent in the considered population.</li>
<li>Alternate Hypothesis -Workclass and salary are dependent in the considered population.</li></ul>
```{r}
chisq.test(adult$Workclass,adult$salary,correct = FALSE)
```
<ul style="list-style-type:disc;"><li>The p-value is less than 0.05, which means that at the 0.05 significance level we fail to accept the null hypothesis that the two categorical variables are independent.Hence ,workclass and salary are dependent on eachother.So,we can use workclass as one of the feature in training for the prediction of salary. </li></ul>
###Analysis of Education and salary
```{r}
ggplot(adult, aes(x=education, group=salary)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop..),
y= ..prop.. ), stat= "count", vjust = -.5,size=3,angle=90) +
labs(y = "Percent", fill="Education") +
facet_grid(~salary) +
scale_y_continuous(labels = scales::percent)+theme(legend.position = 'none',axis.text.x = element_text( hjust = 1,angle=90))+ggtitle("Bar Plot of Salary grouped by Education")
```
<ul style="list-style-type:disc;"><li>Small percentage of school going people earn less equal to 50k whereas a HS-graduate person or a bachelor is more likely to earn more than 50k per year.There are some exceptions like very few people who are studying in 10th or below it but they are still earning greater than 50k.</li></ul>
```{r}
# Number of people having different education earning less than 50K and more than 50K:
table(adult$education,adult$salary)
```
###Test for Independence of Education and salary.
<ul style="list-style-type:disc;">
<li>Null Hypothesis- Education and salary are independent in the considered population.</li>
<li>Alternate Hypothesis -Education and salary are dependent in the considered population.</li></ul>
```{r}
chisq.test(adult$education,adult$salary,correct = FALSE)
```
<ul style="list-style-type:disc;"><li>The p-value is less than 0.05, which means that at the 0.05 significance level we fail to accept the null hypothesis that the two categorical variables are independent.Hence ,Education and salary are dependent on eachother.So,we can use Education as one of the feature in training for the prediction of salary. </li></ul>
###Analysis of education number and salary
```{r}
library(ggplot2)
cdplot(x = adult$educationnum, y = adult$salary,xlab = "Education number", ylab = "Salary", main = "Conditionl Density Plot of Salary vs. Education Number")
```
<ul style="list-style-type:disc;"><li>The conditional density plot of Salary vs. Education number shows that both are correlated,as the education number of person increases his salary also increases.When a person becomes high school graduate there is increase in number of people earning >50k.When a person gets 12 years of education there is rapid increase in number of people earning >50k.</li></ul>
###t-Test
###1)
<ul style="list-style-type:disc;">
<li>Alternate Hypothesis-Average years of education for people earning >50k is more than people who are earning <=50k.</li>
<li>Null Hypothesis-Average years of education for people earning >50k is not more than people who are earning <=50k.</li></ul>
```{r}
gre <- subset(adult$educationnum,adult$salary== ">50k")
less_ <- subset(adult$educationnum,adult$salary=="<=50k")
t.test(gre,less_,paired=FALSE)
```
<ul style="list-style-type:disc;"><li>Conclusion-The p-value is less than 0.05, which means that at the 0.05 significance level we fail to accept the null hypothesis that average years of education for people earning >50k is not more than people who are earning <50k.If a person studies for more number of years is more likely to earn >50k.
</li></ul>
###2)
<ul style="list-style-type:disc;">
<li>Alternate Hypothesis:The increase in number of people earning >50k after passing 10th is more as compared to increase in number of people earning >50k after passing 8th. </li>
<li>Null Hypothesis:The increase in number of people earning >50k after passing 10th is not more as compared to increase in number of people earning >50k after passing 8th. </li></ul>
```{r}
edu <- select(adult,educationnum)
edu <- filter(edu,educationnum >= 8 & educationnum <= 10 )
edu1 <- select(adult,educationnum)
edu1 <- filter(edu,educationnum >= 10 & educationnum <= 12 )
t.test(edu,edu1,paired = FALSE)
```
<ul style="list-style-type:disc;"><li>Conclusion-The p-value is less than 0.05, which means that at the 0.05 significance level we fail to accept the null hypothesis.We accept the alternate hypothesis that the increase in number of people earning >50k after passing 10th is more as compared to increase in number of people earning >50k after passing 8th.
</li></ul>
###3)
<ul style="list-style-type:disc;">
<li>Alternate Hypothesis:There is rapid increase in number of people earning >50k after passing 12th is more as compared to increase in number of people earning >50k after passing 10th. </li>
<li>Null Hypothesis:The increase in number of people earning >50k after passing 12th is not more as compared to increase in number of people earning >50k after passing 10th. </li></ul>
```{r}
edu <- select(adult,educationnum)
edu <- filter(edu,educationnum >= 10 & educationnum <= 12 )
edu1 <- select(adult,educationnum)
edu1 <- filter(edu,educationnum >= 12 & educationnum <= 14)
t.test(edu,edu1)
```
<ul style="list-style-type:disc;"><li>Conclusion-The p-value is less than 0.05, which means that at the 0.05 significance level we fail to accept the null hypothesis.We accept the alternate hypothesis that the increase in number of people earning >50k after passing 12th is more as compared to increase in number of people earning >50k after passing 10th.
</li></ul>
###Analysis of MaritalStatus and salary
```{r}
ggplot(adult, aes(x=maritalstatus, group=salary)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop..),
y= ..prop.. ), stat= "count", vjust = -.5,size=3,angle=90) +
labs(y = "Percent", fill="Marital Status") +
facet_grid(~salary) +
scale_y_continuous(labels = scales::percent)+theme(legend.position = 'none',axis.text.x = element_text( hjust = 1,angle=90))+ggtitle("Bar Plot of Salary grouped by Marital Status")
```
<ul style="list-style-type:disc;"><li>
The Barplot of Salary grouped by marital status shows that a person who has never married is likely to earn less than 50k per year whereas a person who is married to a civilian spouse is likely to earn more than 50k per year.Very few participants are married to army force spouse and 0.1% of them earn more than 50k per year as well as 0.1% earns <=50k.
</li></ul>
```{r}
# Number of people having different marital status earning less than 50K and more than 50K:
table(adult$maritalstatus,adult$salary)
```
###Test for Independence of MaritalStatus and salary.
<ul style="list-style-type:disc;"><li>
Null Hypothesis- MaritalStatus and salary are independent in the considered population.</li>
<li>Alternate Hypothesis -MaritalStatus and salary are dependent in the considered population.</li></ul>
```{r}
chisq.test(adult$maritalstatus,adult$salary,correct = FALSE)
```
<ul style="list-style-type:disc;"><li>The p-value is less than 0.05, which means that at the 0.05 significance level we fail to accept the null hypothesis that the two categorical variables are independent.Hence ,MaritalStatus and salary are dependent on eachother.So,we can use MaritalStatus as one of the feature in training for the prediction of salary. </li></ul>
###Analysis of Occupation and salary
```{r}
ggplot(adult, aes(x=occupation, group=salary)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop..),
y= ..prop.. ), stat= "count", vjust = -.5,size=3.5,angle=90) +
labs(y = "Percent", fill="Occupation") +
facet_grid(~salary) +
scale_y_continuous(labels = scales::percent)+theme(legend.position = 'none',axis.text.x = element_text( hjust = 1,angle=90))+ggtitle("Bar Plot of Salary grouped by Occupation")
```
<ul style="list-style-type:disc;"><li>A participant who has speciality in a particular profession or a person who is executive manager is likely to earn more than 50k per year .Maximum number of participants who are engaged in "Pro-speciality" earns <=50k due to their less age or less number of working hours.</li></ul>
```{r}
# Number of people in different occupation earning less than 50K and more than 50K:
table(adult$occupation,adult$salary)
```
###Test for Independence of Occupation and salary.
<ul style="list-style-type:disc;"><li>Null Hypothesis- Occupation and salary are independent in the considered population.</li>
<li>Alternate Hypothesis -Occupation and salary are dependent in the considered population.</li>
```{r message=FALSE, warning=FALSE, paged.print=FALSE}
chisq.test(adult$occupation,adult$salary,correct = FALSE)
```
<ul style="list-style-type:disc;"><li>The p-value is less than 0.05, which means that at the 0.05 significance level we fail to accept the null hypothesis that the two categorical variables are independent.Hence ,Occupation and salary are dependent on eachother.So,we can use Occupation as one of the feature in training for the prediction of salary. </li></ul>
###Analysis of Relationship and salary
```{r}
ggplot(adult, aes(x=relationship, group=salary)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop..),
y= ..prop.. ), stat= "count", vjust = -.5,size=3) +
labs(y = "Percent", fill="Relationship",x = "Relationship") +
facet_grid(~salary) +
scale_y_continuous(labels = scales::percent)+theme(legend.position = 'none',axis.text.x = element_text( hjust = 1,angle=90))+ggtitle("Bar Plot of Salary grouped by Relationship")
```
<ul style="list-style-type:disc;"><li>A person who is not in a family is likely to earn to less than 50k year as maximum people earning less than 50k a year are high school graduate or they maybe separated.Ratio between husbands earning <=50k to husbands earning >50k is very less that is about 0.4% which shows majority of working people are males and are husbands as husbands have responsibility to earn.</li></ul>
```{r}
# Number of people having different relationship with others earning less than 50K and more than 50K:
table(adult$relationship,adult$salary)
```
```{r}
ggplot(data = adult, mapping = aes(x = adult$maritalstatus,fill=relationship)) + geom_bar() +ggtitle("Barplot of Relationship grouped by marital status")+theme(axis.text.x = element_text( hjust = 1,angle=90))+labs(x="Marital Status",y="Relationship")
```
<ul style="list-style-type:disc;"><li>
Relationship is closely realted to the variable " marital status" and they should be considered together. All the individuals of the category" Not-in-family" have a proper marital status, i.e. " Divorced"," Married-spouse-absent"," Never-married"," Separated" or " Widowed". The situation is similar with the categories" Husband" and " Wife", where there are no discrepancies between their marital and relationship status.Another interesting observation that can be made is that people with marital status" Never-married" recognized themselves as belonging to one of the four relationship categories - " Not-in-family"," Unmarried"," Other-relative" and " Own-child".
</li></ul>
###Test for Independence of Relationship and salary.
<ul style="list-style-type:disc;"><li>Null Hypothesis- Relationship and salary are independent in the considered population.</li>
<li>Alternate Hypothesis -Relationship and salary are dependent in the considered population.</li></ul>
```{r}
chisq.test(adult$relationship,adult$salary,correct = FALSE)
```
<ul style="list-style-type:disc;"><li>The p-value is less than 0.05, which means that at the 0.05 significance level we fail to accept the null hypothesis that the two categorical variables are independent.Hence ,Relationship and salary are dependent on eachother.So,we can use Relationship as one of the feature in training for the prediction of salary.</li></ul>
###Analysis of Race and salary
```{r}
ggplot(adult, aes(x=race, group=salary)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop..),
y= ..prop.. ), stat= "count", vjust = -.5,size=3.4,angle=90) +
labs(y = "Percent", fill="Racep",x = "Race") +
facet_grid(~salary) +
scale_y_continuous(labels = scales::percent)+theme(legend.position = 'none',axis.text.x = element_text( hjust = 1,angle=90))+ggtitle("Bar Plot of Salary grouped by Race")
```
<ul style="list-style-type:disc;"><li>The ratio between white and black people earning less than 50k is about 8% whereas this ratio grows to 18% for people earning greater than 50k per year.This difference in ratio shows more white people earn >50k.</li></ul>
```{r}
# Number of people from different "race" earning less than 50K and more than 50K:
table(adult$race,adult$salary)
```
###Test for Independence of Race and salary.
<ul style="list-style-type:disc;"><li>Null Hypothesis- Race and salary are independent in the considered population.</li>
<li>Alternate Hypothesis -Race and salary are dependent in the considered population.</li></ul>
```{r}
chisq.test(adult$race,adult$salary,correct = FALSE)
```
<ul style="list-style-type:disc;"><li>The p-value is less than 0.05, which means that at the 0.05 significance level we fail to accept the null hypothesis that the two categorical variables are independent.Hence ,Race and salary are dependent on eachother.So,we can use Race as one of the feature in training for the prediction of salary. </li></ul>
###Analysis of sex and salary
```{r}
ggplot(adult, aes(x=sex, group=salary)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
geom_text(aes( label = scales::percent(..prop..),
y= ..prop.. ), stat= "count", vjust = -.5,size=3.5) +
labs(y = "Percent", fill="gender",x = "gender") +
facet_grid(~salary) +
scale_y_continuous(labels = scales::percent)+theme(legend.position = 'none',axis.text.x = element_text( hjust = 1,angle=90))+ggtitle("Bar Plot of Salary grouped by gender")
```
<ul style="list-style-type:disc;"><li>
The ratio of male to female who are earning less than 50k is just 1.5 whereas this ratio grows 5.6 for male to female earning greater than 50k.Females earning >50k is very less due to less years of education or less working hours than men.</li></ul>
```{r}
# Number of men and women earning less than 50K and more than 50K:
table(adult$sex,adult$salary)
```
###Plot of Gender and education number
```{r}
ggplot(data = adult, mapping = aes(x = educationnum)) + geom_histogram(binwidth = 5,color = "black",fill = "lightblue",alpha = 0.6) +scale_x_continuous(breaks = seq(0, 95, 5)) + facet_wrap(~sex) +ggtitle("Histogram of Education number by Gender")
```
<ul style="list-style-type:disc;"><li>Above plot shows that average years of education of majority of females is very less than males,thus majority of females are earning less than 50k.Most of the males are educated so working population of male is also more.</li></ul>
###Test for Independence of Gender and salary.
<ul style="list-style-type:disc;"><li>Null Hypothesis- Gender and salary are independent in the considered population.</li>
<li>Alternate Hypothesis -Gender and salary are dependent in the considered population.</li></ul>
```{r}
chisq.test(adult$sex,adult$salary,correct = FALSE)
```
<ul style="list-style-type:disc;"><li>The p-value is less than 0.05, which means that at the 0.05 significance level we fail to accept the null hypothesis that the two categorical variables are independent.Hence ,Gender and salary are dependent on eachother.So,we can use Gender as one of the feature in training for the prediction of salary. </li></ul>
###Analysis of Capital Gain and salary
```{r}
ggplot(adult,aes(x=capitalgain,fill=salary))+geom_histogram(position="dodge",binwidth = 6000)+scale_y_continuous(breaks = seq(0, 30000, 2000))+ggtitle("Salary grouped by capital gain")+facet_wrap(~salary)
```
<ul style="list-style-type:disc;"><li>
Capital gain have very few unique values, although majority of their instances have zero values. This implies that very few people had capital gain whether they earn >50k or less than 50k,that shows capital gain doesnot have significant affect on distribution of salary. </li></ul>
###Analysis of Capital loss and salary
```{r}
ggplot(adult,aes(x=capitalloss,fill=salary))+geom_histogram(binwidth = 500,position="dodge")+scale_y_continuous(breaks = seq(0, 30000, 2000))+
scale_x_continuous(breaks = seq(0, 3000, 500))+ggtitle("Salary grouped by capitalloss")
```
<ul style="list-style-type:disc;"><li>Capital loss also have very few unique values, although majority of their instances have zero values. This implies that very few people had capital loss whether they earn >50k or less than 50k,that shows capital loss doesnot have significant affect on distribution of salary.
</li></ul>
###Analysis of Hours per week and salary
```{r}
ggplot(aes(x = salary, y = hoursperweek),data = adult) +geom_boxplot() +stat_summary(fun.y = mean,geom = "point", shape = 19,color = "red",cex = 2) +coord_cartesian(ylim = c(20, 80)) +scale_x_discrete(breaks = NULL) +scale_y_continuous(breaks = seq(20,80, 5)) + ylab("Hours per Week") +xlab("Salary") + facet_wrap(.~salary)+ggtitle("Box plot of Salary grouped by Hours per Week")
```
<ul style="list-style-type:disc;"><li>People who earn less than 50k a year works for 40 hours on an average in a week whereas average working hours of people earning >50k is 45.Thus we can say people who work for more number of hours in a week earns more than 50k whereas people who work for less hours in a week earns less than 50k per year.Ususally people who are studying or who are of older age or women works for less hours in a week thus they earn less than 50k.</li>
<li>Even there are people who are working for more 50 hours to about 80 hours but still earns <=50k whereas there are people who works for <27 hours a week but still earn <=50k.</li>
<li>There are people who have to work for more than 65 hours to earn >50k per year.</li>
<li>Some people works for less than 27 hours but still earns >50k a year which shows the impact of more education and experience.</li>
</ul>