Last active
July 17, 2018 13:20
Revisions
-
Henrik Lindberg revised this gist
Apr 30, 2017 . 2 changed files with 533 additions and 0 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ # Data from https://www.kaggle.com/bls/american-time-use-survey library(tidyverse) library(tidytext) This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,531 @@ code;name 01;Personal Care Activities 0101;Sleeping 0102;Grooming 0103;Health-related self care 0104;Personal Activities 0105;Personal Care Emergencies 0199;Personal care, n.e.c.* 02;Household Activities 0201;Housework 0202;Food & Drink Preparation, Presentation, & Clean-up 0203;Interior Maintenance, Repair & Decoration 0204;Exterior Maintenance, Repair & Decoration 0205;Lawn, Garden, and Houseplants 0206;Animals and Pets 0207;Vehicles 0208;Appliances, Tools, and Toys 0209;Household Management 0299;Household activities, n.e.c.* 03;Caring For & Helping Household (HH) Members 0301;Caring For & Helping HH Children 0302;Activities Related to HH Children's Education 0303;Activities Related to HH Children's Health 0304;Caring for Household Adults 0305;Helping Household Adults 0399;Caring For & Helping NonHH Members 04;Caring for & Helping Nonhousehold (NonHH) Members 0401;Caring for & Helping Nonhousehold Children 0402;Activities Related to Nonhh Children's Education 0403;Activities Related to Nonhh Children's Health 0404;Caring For Nonhousehold Adults 0405;Helping Nonhousehold Adults 0499;Caring For & Helping Nonhousehold Adults, n.e.c.* 05;Work & Work-Related Activities 0501;Working 0502;Work-Related Activities 0503;Other Income-Generating Activities 0504;Job Search and Interviewing 0599;Work and Work-Related Activities, n.e.c.* 06;Education 0601;Taking Class 0602;Extracurricular School Activities (Except Sports) 0603;Research/Homework 0604;Registration/Administrative activities 0699;Education, n.e.c.* 07;Consumer Purchases 0701;Shopping (Store, Telephone, Internet) 0702;Researching Purchases 0703;Security Procedures Related to Consumer Purchases 0799;Consumer Purchases, n.e.c.* 08;Professional & Personal Care Services 0801;Professional & Personal Care Services 0802;Financial Services and Banking 0803;Legal Services 0804;Medical and Care Services 0805;Personal Care Services 0806;Real Estate 0807;Veterinary Services (excluding grooming) 0808;Security Procedures Related to Professional/Personal Services 0899;Professional and Personal Services, n.e.c.* 09;Household Services 0901;Household Services (not done by self) 0902;Home Maintenance/Repair/Décor/Construction (not done by self) 0903;Pet Services (not done by self, not vet) 0904;Lawn & Garden Services (not done by self) 0905;Vehicle Maintenance & Repair Services (not done by self) 0999;Household Services, n.e.c.* 10;Government Services & Civic Obligations 1001;Using Government Services 1002;Civic Obligations & Participation 1003;Waiting Associated w/ Government Services or Civic Obligations 1004;Security Procedures Related to Government Services or Civic Obligations 1099;Government services, n.e.c.* 11;Eating and Drinking 1101;Eating and Drinking 1102;Waiting associated with Eating & Drinking 1199;Eating and Drinking, n.e.c.* 12;Socializing, Relaxing, and Leisure 1201;Socializing and Communicating 1202;Attending or Hosting Social Events 1203;Relaxing and Leisure 1204;Arts and Entertainment (other than sports) 1205;Waiting associated with Socializing, Relaxing, and Leisure 1299;Socializing, Relaxing, and Leisure, n.e.c.* 13;Sports, Exercise, & Recreation 1301;Participating in Sports, Exercise, and Recreation 1302;Attending Sports/Recreational Events 1303;Waiting Associated with Sports, Exercise, & Recreation 1304;Security Procedures Related to Sports, Exercise, & Recreation 1399;Sports, Exercise, and Recreation, n.e.c.* 14;Religious and Spiritual Activities 1401;Religious/Spiritual Practices 1499;Religious and Spiritual Practices, n.e.c.* 15;Volunteer Activities 1501;Administrative & Support Activities 1502;Social Service & Care Activities (Except Medical) 1503;Indoor & Outdoor Maintenance, Building, & Clean-up Activities 1504;Participating in Performance & Cultural Activities 1505;Attending Meetings, Conferences, & Training 1506;Public Health & Safety Activities 1599;Volunteer Activities, n.e.c.* 16;Telephone Calls 1601;Telephone Calls (to or from) 1699;Telephone Calls, n.e.c.* 18;Traveling 1801;Travel Related to Personal Care 1802;Travel Related to Household Activities 1803;Travel Related to Caring For & Helping Household Members 1804;Travel Related to Caring For & Helping Nonhousehold Members 1805;Travel Related to Work 1806;Travel Related to Education 1807;Travel Related to Consumer Purchases 1808;Travel Related to Using Professional and Personal Care Services 1809;Travel Related to Using Household Services 1810;Travel Related to Using Government Services & Civic Obligations 1811;Travel Related to Eating and Drinking 1812;Travel Related to Socializing, Relaxing, and Leisure 1813;Travel Related to Sports, Exercise, and Recreation 50;Data Codes 5001;Unable to Code 5099;Data codes, n.e.c.* 010101;Sleeping 010102;Sleeplessness 010199;Sleeping, n.e.c.* 010201;Washing, dressing and grooming oneself 010299;Grooming, n.e.c.* 010301;Health-related self care 010399;Self care, n.e.c.* 010401;Personal/Private activities 010499;Personal activities, n.e.c.* 010501;Personal emergencies 010599;Personal care emergencies, n.e.c.* 019999;Personal care, n.e.c.* 020101;Interior cleaning 020102;Laundry 020103;Sewing, repairing, & maintaining textiles 020104;Storing interior hh items, inc. food 020199;Housework, n.e.c.* 020201;Food and drink preparation 020202;Food presentation 020203;Kitchen and food clean-up 020299;Food & drink prep, presentation, & clean-up, n.e.c.* 020301;Interior arrangement, decoration, & repairs 020302;Building and repairing furniture 020303;Heating and cooling 020399;Interior maintenance, repair, & decoration, n.e.c.* 020401;Exterior cleaning 020402;Exterior repair, improvements, & decoration 020499;Exterior maintenance, repair & decoration, n.e.c.* 020501;Lawn, garden, and houseplant care 020502;Ponds, pools, and hot tubs 020599;Lawn and garden, n.e.c.* 020681;Care for animals and pets (not veterinary care) 020699;Pet and animal care, n.e.c.* 020701;Vehicle repair and maintenance (by self) 020799;Vehicles, n.e.c.* 020801;Appliance, tool, and toy set-up, repair, & maintenance (by self) 020899;Appliances and tools, n.e.c.* 020901;Financial management 020902;Household & personal organization and planning 020903;HH & personal mail & messages (except e-mail) 020904;HH & personal e-mail and messages 020905;Home security 020999;Household management, n.e.c.* 029999;Household activities, n.e.c.* 030101;Physical care for hh children 030102;Reading to/with hh children 030103;Playing with hh children, not sports 030104;Arts and crafts with hh children 030105;Playing sports with hh children 030186;Talking with/listening to hh children 030108;Organization & planning for hh children 030109;Looking after hh children (as a primary activity) 030110;Attending hh children's events 030111;Waiting for/with hh children 030112;Picking up/dropping off hh children 030199;Caring for & helping hh children, n.e.c.* 030201;Homework (hh children) 030202;Meetings and school conferences (hh children) 030203;Home schooling of hh children 030204;Waiting associated with hh children's education 030299;Activities related to hh child's education, n.e.c.* 030301;Providing medical care to hh children 030302;Obtaining medical care for hh children 030303;Waiting associated with hh children's health 030399;Activities related to hh child's health, n.e.c.* 030401;Physical care for hh adults 030402;Looking after hh adult (as a primary activity) 030403;Providing medical care to hh adult 030404;Obtaining medical and care services for hh adult 030405;Waiting associated with caring for household adults 030499;Caring for household adults, n.e.c.* 030501;Helping hh adults 030502;Organization & planning for hh adults 030503;Picking up/dropping off hh adult 030504;Waiting associated with helping hh adults 030599;Helping household adults, n.e.c.* 039999;Caring for & helping hh members, n.e.c.* 040101;Physical care for nonhh children 040102;Reading to/with nonhh children 040103;Playing with nonhh children, not sports 040104;Arts and crafts with nonhh children 040105;Playing sports with nonhh children 040186;Talking with/listening to nonhh children 040108;Organization & planning for nonhh children 040109;Looking after nonhh children (as primary activity) 040110;Attending nonhh children's events 040111;Waiting for/with nonhh children 040112;Dropping off/picking up nonhh children 040199;Caring for and helping nonhh children, n.e.c.* 040201;Homework (nonhh children) 040202;Meetings and school conferences (nonhh children) 040203;Home schooling of nonhh children 040204;Waiting associated with nonhh children's education 040299;Activities related to nonhh child's educ., n.e.c.* 040301;Providing medical care to nonhh children 040302;Obtaining medical care for nonhh children 040303;Waiting associated with nonhh children's health 040399;Activities related to nonhh child's health, n.e.c.* 040401;Physical care for nonhh adults 040402;Looking after nonhh adult (as a primary activity) 040403;Providing medical care to nonhh adult 040404;Obtaining medical and care services for nonhh adult 040405;Waiting associated with caring for nonhh adults 040499;Caring for nonhh adults, n.e.c.* 040501;Housework, cooking, & shopping assistance for nonhh adults 040502;House & lawn maintenance & repair assistance for nonhh adults 040503;Animal & pet care assistance for nonhh adults 040504;Vehicle & appliance maintenance/repair assistance for nonhh adults 040505;Financial management assistance for nonhh adults 040506;Household management & paperwork assistance for nonhh adults 040507;Picking up/dropping off nonhh adult 040508;Waiting associated with helping nonhh adults 049999;Caring for & helping nonhh members, n.e.c.* 050101;Work, main job 050102;Work, other job(s) 050103;Security procedures related to work 050189;Working, n.e.c.* 050201;Socializing, relaxing, and leisure as part of job 050202;Eating and drinking as part of job 050203;Sports and exercise as part of job 050204;Security procedures as part of job 050301;Income-generating hobbies, crafts, and food 050302;Income-generating performances 050303;Income-generating services 050304;Income-generating rental property activities 050389;Other income-generating activities, n.e.c.* 050481;Job search activities 050403;Job interviewing 050404;Waiting associated with job search or interview 050405;Security procedures rel. to job search/interviewing 059999;Work and work-related activities, n.e.c.* 060101;Taking class for degree, certification, or licensure 060102;Taking class for personal interest 060103;Waiting associated with taking classes 060104;Security procedures rel. to taking classes 060199;Taking class, n.e.c.* 060201;Extracurricular club activities 060202;Extracurricular music & performance activities 060203;Extracurricular student government activities 060289;Education-related extracurricular activities, n.e.c.* 060301;Research/homework for class for degree, certification, or licensure 060302;Research/homework for class for pers. interest 060303;Waiting associated with research/homework 060401;Administrative activities: class for degree, certification, or licensure 060403;Waiting associated w/admin. activities (education) 060499;Administrative for education, n.e.c.* 069999;Education, n.e.c.* 070101;Grocery shopping 070102;Purchasing gas 070103;Purchasing food (not groceries) 070104;Shopping, except groceries, food and gas 070105;Waiting associated with shopping 070199;Shopping, n.e.c.* 070201;Comparison shopping 070301;Security procedures rel. to consumer purchases 070399;Security procedures rel. to consumer purchases, n.e.c.* 079999;Consumer purchases, n.e.c.* 080101;Using paid childcare services 080102;Waiting associated w/purchasing childcare svcs 080199;Using paid childcare services, n.e.c.* 080201;Banking 080202;Using other financial services 080203;Waiting associated w/banking/financial services 080299;Using financial services and banking, n.e.c.* 080301;Using legal services 080302;Waiting associated with legal services 080399;Using legal services, n.e.c.* 080401;Using health and care services outside the home 080402;Using in-home health and care services 080403;Waiting associated with medical services 080499;Using medical services, n.e.c.* 080501;Using personal care services 080502;Waiting associated w/personal care services 080599;Using personal care services, n.e.c.* 080601;Activities rel. to purchasing/selling real estate 080602;Waiting associated w/purchasing/selling real estate 080699;Using real estate services, n.e.c.* 080701;Using veterinary services 080702;Waiting associated with veterinary services 080801;Security procedures rel. to professional/personal svcs. 080899;Security procedures rel. to professional/personal svcs n.e.c.* 089999;Professional and personal services, n.e.c.* 090101;Using interior cleaning services 090102;Using meal preparation services 090103;Using clothing repair and cleaning services 090104;Waiting associated with using household services 090199;Using household services, n.e.c.* 090201;Using home maint/repair/décor/construction svcs 090202;Waiting associated w/ home main/repair/décor/constr 090299;Using home maint/repair/décor/constr services, n.e.c.* 090301;Using pet services 090302;Waiting associated with pet services 090399;Using pet services, n.e.c.* 090401;Using lawn and garden services 090402;Waiting associated with using lawn & garden services 090499;Using lawn and garden services, n.e.c.* 090502;Waiting associated with vehicle main. or repair svcs 090599;Using vehicle maint. & repair svcs, n.e.c.* 099999;Using household services, n.e.c.* 100101;Using police and fire services 100102;Using social services 100103;Obtaining licenses & paying fines, fees, taxes 100199;Using government services, n.e.c.* 100201;Civic obligations & participation 100299;Civic obligations & participation, n.e.c.* 100381;Waiting associated with using government services 100383;Waiting associated w/civic obligations & participation 100399;Waiting assoc. w/govt svcs or civic obligations, n.e.c.* 100401;Security procedures rel. to govt svcs/civic obligations 100499;Security procedures rel. to govt svcs/civic obligations, n.e.c.* 109999;Government services, n.e.c.* 110101;Eating and drinking 110199;Eating and drinking, n.e.c.* 110281;Waiting associated w/eating & drinking 110289;Waiting associated with eating & drinking, n.e.c.* 119999;Eating and drinking, n.e.c.* 120101;Socializing and communicating with others 120199;Socializing and communicating, n.e.c.* 120201;Attending or hosting parties/receptions/ceremonies 120202;Attending meetings for personal interest (not volunteering) 120299;Attending/hosting social events, n.e.c.* 120301;Relaxing, thinking 120302;Tobacco and drug use 120303;Television and movies (not religious) 120304;Television (religious) 120305;Listening to the radio 120306;Listening to/playing music (not radio) 120307;Playing games 120308;Computer use for leisure (exc. Games) 120309;Arts and crafts as a hobby 120310;Collecting as a hobby 120311;Hobbies, except arts & crafts and collecting 120312;Reading for personal interest 120313;Writing for personal interest 120399;Relaxing and leisure, n.e.c.* 120401;Attending performing arts 120402;Attending museums 120403;Attending movies/film 120404;Attending gambling establishments 120405;Security procedures rel. to arts & entertainment 120499;Arts and entertainment, n.e.c.* 120502;Waiting assoc. w/attending/hosting social events 120503;Waiting associated with relaxing/leisure 120504;Waiting associated with arts & entertainment 120599;Waiting associated with socializing, n.e.c.* 129999;Socializing, relaxing, and leisure, n.e.c.* 130101;Doing aerobics 130102;Playing baseball 130103;Playing basketball 130104;Biking 130105;Playing billiards 130106;Boating 130107;Bowling 130108;Climbing, spelunking, caving 130109;Dancing 130110;Participating in equestrian sports 130111;Fencing 130112;Fishing 130113;Playing football 130114;Golfing 130115;Doing gymnastics 130116;Hiking 130117;Playing hockey 130118;Hunting 130119;Participating in martial arts 130120;Playing racquet sports 130121;Participating in rodeo competitions 130122;Rollerblading 130123;Playing rugby 130124;Running 130125;Skiing, ice skating, snowboarding 130126;Playing soccer 130127;Softball 130128;Using cardiovascular equipment 130129;Vehicle touring/racing 130130;Playing volleyball 130131;Walking 130132;Participating in water sports 130133;Weightlifting/strength training 130134;Working out, unspecified 130135;Wrestling 130136;Doing yoga 130199;Playing sports n.e.c.* 130201;Watching aerobics 130202;Watching baseball 130203;Watching basketball 130204;Watching biking 130205;Watching billiards 130206;Watching boating 130207;Watching bowling 130208;Watching climbing, spelunking, caving 130209;Watching dancing 130210;Watching equestrian sports 130211;Watching fencing 130212;Watching fishing 130213;Watching football 130214;Watching golfing 130215;Watching gymnastics 130216;Watching hockey 130217;Watching martial arts 130218;Watching racquet sports 130219;Watching rodeo competitions 130220;Watching rollerblading 130221;Watching rugby 130222;Watching running 130223;Watching skiing, ice skating, snowboarding 130224;Watching soccer 130225;Watching softball 130226;Watching vehicle touring/racing 130227;Watching volleyball 130228;Watching walking 130229;Watching water sports 130230;Watching weightlifting/strength training 130231;Watching people working out, unspecified 130232;Watching wrestling 130301;Waiting related to playing sports or exercising 130302;Waiting related to attending sporting events 130399;Waiting associated with sports, exercise, & recreation, n.e.c.* 130401;Security related to playing sports or exercising 130402;Security related to attending sporting events 130499;Security related to sports, exercise, & recreation, n.e.c.* 140101;Attending religious services 140102;Participation in religious practices 140103;Waiting associated w/religious & spiritual activities 140104;Security procedures rel. to religious & spiritual activities 140105;Religious education activities 149999;Religious and spiritual activities, n.e.c.* 150101;Computer use 150102;Organizing and preparing 150103;Reading 150104;Telephone calls (except hotline counseling) 150105;Writing 150106;Fundraising 150199;Administrative & support activities, n.e.c.* 150201;Food preparation, presentation, clean-up 150202;Collecting & delivering clothing & other goods 150203;Providing care 150204;Teaching, leading, counseling, mentoring 150299;Social service & care activities, n.e.c.* 150301;Building houses, wildlife sites, & other structures 150302;Indoor & outdoor maintenance, repair, & clean-up 150399;Indoor & outdoor maintenance, building & clean-up activities, n.e.c.* 150401;Performing 150499;Participating in performance & cultural activities, n.e.c.* 150501;Attending meetings, conferences, & training 150599;Attending meetings, conferences, & training, n.e.c.* 150601;Public health activities 150602;Public safety activities 150699;Public health & safety activities, n.e.c.* 159989;Volunteer activities, n.e.c.* 160101;Telephone calls to/from family members 160102;Telephone calls to/from friends, neighbors, or acquaintances 160103;Telephone calls to/from education services providers 160104;Telephone calls to/from salespeople 160105;Telephone calls to/from professional or personal care svcs providers 160106;Telephone calls to/from household services providers 160108;Telephone calls to/from government officials 169989;Telephone calls, n.e.c.* 180101;Travel related to personal care 180199;Travel related to personal care, n.e.c.* 180280;Travel related to household activities 180381;Travel related to caring for and helping hh children 180382;Travel related to caring for and helping hh adults 180399;Travel rel. to caring for & helping hh members, n.e.c.* 180481;Travel related to caring for and helping nonhh children 180482;Travel related to caring for and helping nonhh adults 180499;Travel rel. to caring for & helping nonhh members, n.e.c.* 180501;Travel related to working 180502;Travel related to work-related activities 180589;Travel related to work, n.e.c.* 180601;Travel related to taking class 180699;Travel related to education, n.e.c.* 180701;Travel related to grocery shopping 180782;Travel related to shopping (except grocery shopping) 180801;Travel related to using childcare services 180802;Travel related to using financial services and banking 180803;Travel related to using legal services 180804;Travel related to using medical services 180805;Travel related to using personal care services 180806;Travel related to using real estate services 180807;Travel related to using veterinary services 180899;Travel rel. to using prof. & personal care services, n.e.c.* 180901;Travel related to using household services 180902;Travel related to using home main./repair/décor./construction svcs 180903;Travel related to using pet services (not vet) 180904;Travel related to using lawn and garden services 180905;Travel related to using vehicle maintenance & repair services 180999;Travel related to using household services, n.e.c.* 181002;Travel related to civic obligations & participation 181099;Travel rel. to govt svcs & civic obligations, n.e.c.* 181101;Travel related to eating and drinking 181199;Travel related to eating and drinking, n.e.c.* 181201;Travel related to socializing and communicating 181202;Travel related to attending or hosting social events 181283;Travel related to relaxing and leisure 181204;Travel related to arts and entertainment 181299;Travel rel. to socializing, relaxing, & leisure, n.e.c.* 181301;Travel related to participating in sports/exercise/recreation 181302;Travel related to attending sporting/recreational events 181399;Travel related to sports, exercise, & recreation, n.e.c.* 181401;Travel related to religious/spiritual practices 181499;Travel rel. to religious/spiritual activities, n.e.c.* 181501;Travel related to volunteering 181599;Travel related to volunteer activities, n.e.c.* 181601;Travel related to phone calls 181699;Travel rel. to phone calls, n.e.c.* 181801;Security procedures related to traveling 181899;Security procedures related to traveling, n.e.c.* 189999;Traveling, n.e.c.* 509989;Insufficient detail in verbatim -
halhen created this gist
Apr 30, 2017 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,147 @@ library(tidyverse) library(tidytext) # Contains both 1st and 2nd level codes; collisions won't appear though since 1-99 is tier 1 and 100+ is two df.names <- read_csv2('../data/atus/codes.csv') %>% mutate(code = as.integer(code)) df.resp <- read_csv('../data/atus/atusresp.csv') df.act <- read_csv('../data/atus/atusact.csv', col_types=cols(tustarttim = col_character(), tustoptime = col_character())) df.sum <- read_csv('../data/atus/atussum.csv') df <- df.act %>% select(tucaseid, tuactivity_n, trcodep, trtier1p, trtier2p, tuactdur24) %>% left_join(df.names %>% rename(activity = name), by=c('trcodep' = 'code')) %>% left_join(df.sum %>% select(tucaseid, tesex, teage, tudiaryday, tryhhchild, tuyear, trsppres), by='tucaseid') # Summary stats df %>% group_by(activity) %>% summarize(n=n()) %>% arrange(-n) # tf-idf df %>% filter(teage >= 25, teage <= 45) %>% filter(trtier1p != 18, trtier1p != 50) %>% mutate(gender = ifelse(tesex==1, 'Male', 'Female'), kids = ifelse(tryhhchild >= 0, 'Kids', 'No kids'), group = paste0(kids, ' - ', gender)) %>% group_by(gender, group, activity) %>% summarize(n=n()) %>% filter(n >= 20) %>% bind_tf_idf(activity, group, n) %>% group_by(group) %>% top_n(10, tf_idf) %>% ggplot(aes(reorder(activity, tf_idf), tf_idf, fill=gender)) + geom_bar(stat='identity', show.legend=FALSE) + scale_y_continuous(breaks=NULL) + coord_flip() + labs(x="", y="", title="Particular activities by gender and parenthood, 25-45 year olds", caption="@hnrklndbrg | Source: American Time Use Survey") + facet_wrap(~ group, scales='free', ncol=2) + hrbrthemes::theme_ipsum(grid='') ggsave('/tmp/gender.png', width=12, height=6) df %>% filter(trtier1p != 18, trtier1p != 50) %>% filter(teage < 85) %>% mutate(age.group = trunc((teage + 5) / 10) * 10, group = paste0(age.group - 5, '-', age.group + 4)) %>% group_by(trtier1p, group, activity) %>% summarize(n=n()) %>% ungroup() %>% filter(n >= 20) %>% bind_tf_idf(activity, group, n) %>% group_by(group) %>% top_n(10, tf_idf) %>% left_join(df.names %>% mutate(category = name), by=c('trtier1p'='code')) %>% ggplot(aes(reorder(activity, tf_idf), tf_idf, fill=category)) + geom_bar(stat='identity') + scale_y_continuous(breaks=NULL) + coord_flip() + labs(x="", y="", title="Particular activities by age", caption="@hnrklndbrg | Source: American Time Use Survey") + scale_fill_discrete(name='') + facet_wrap(~ group, scales='free', ncol=2) + hrbrthemes::theme_ipsum(grid='') + theme(legend.position=c(0.7, 0.1)) ggsave('/tmp/age.png', width=12, height=12) # LDA df.tmp <- df %>% # exclude travel filter(trtier1p != 18, trtier1p != 50) %>% filter(tuyear >= 2013) %>% #thursday filter(tudiaryday == 5) %>% group_by(tucaseid, activity) %>% summarize(n=sum(tuactdur24)) set.seed(1234) lda <- df.tmp %>% cast_dtm(tucaseid, activity, n) %>% topicmodels::LDA(k=5) # Classify and join with summary file to get demographics df.tmp2 <- tidy(lda, matrix='gamma') %>% group_by(document) %>% filter(gamma == max(gamma)) %>% left_join(df.sum %>% mutate(document = as.character(tucaseid)), by='document') (p.demo <- df.tmp2 %>% filter(teage < 85) %>% mutate(age.group = trunc((teage + 5) / 10) * 10, age.group = paste0(age.group - 5, '-', age.group + 4)) %>% transmute(topic, gender = ifelse(tesex == 1, 'Male', 'Female'), age = age.group, kids = ifelse(tryhhchild >= 0, 'Kids', 'No kids')) %>% gather(key, value, gender:kids) %>% group_by(topic, key, value) %>% summarize(n=n()) %>% mutate(p=n/sum(n)) %>% ggplot(aes(value, p, fill=factor(topic))) + geom_bar(stat='identity', show.legend=FALSE) + scale_y_continuous(labels=scales::percent) + facet_grid(topic~key, scales='free_x', space='free_x') + labs(x="", y="") + hrbrthemes::theme_ipsum(grid='Y') + theme(strip.text=element_blank())) (p.dim <- lda %>% tidy(matrix='beta') %>% filter(term != 'Sleeping') %>% group_by(topic) %>% top_n(10, beta) %>% ggplot(aes(reorder(term, beta), beta, fill=factor(topic))) + geom_bar(stat='identity', show.legend=FALSE) + coord_flip() + scale_y_continuous(labels=function(x) {rep("", length(x))}) + labs(x="", y="") + facet_grid(topic~., scales='free') + hrbrthemes::theme_ipsum(grid='') + theme(strip.text=element_blank())) (p <- gridExtra::grid.arrange(p.dim, p.demo, ncol=2)) ggsave('/tmp/lda.png', plot=p, width=12, height=10)