Created
May 10, 2020 18:16
-
-
Save calvinfroedge/14700b3cd027d1d2acc577269c48cc57 to your computer and use it in GitHub Desktop.
apple covid 19 analysis may 8 2020
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 characters
// Download data from https://www.apple.com/covid19/mobility | |
const csv=require('csvtojson') | |
const _=require('lodash'); | |
let getDateKeys = (month, days, startDate=1)=>{ | |
let keys = []; | |
for(var i=startDate;i<days+1;i++){ | |
keys.push(`2020-0${month}-${i < 10 ? '0' : ''}${i}`) | |
} | |
return keys; | |
} | |
const converter=csv() | |
.fromFile('./applemobilitytrends-2020-05-08.csv') | |
.then((json)=>{ | |
let countryDiffs = []; | |
let months = [ | |
[1, 31, 13], | |
[2, 29], | |
[3, 31], | |
[4, 30], | |
[5, 8] | |
].map(group => getDateKeys(...group)); | |
let byCountry = json.filter(item => item.geo_type == 'country/region' && item.transportation_type == 'driving').map((item)=>{ | |
let daily = []; | |
let monthAverages = months.map((month)=>{ | |
let days = month.map((day)=>{ | |
let diff = item[day] - 100; | |
daily.push(diff); | |
return diff; | |
}) | |
let avg = Number(_.mean(days).toFixed(2)); | |
return avg; | |
}); | |
let today = item['2020-05-08'] - 100; | |
console.log(`${item.region}`); | |
let monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May']; | |
console.log(`Today: ${today.toFixed(2)}%\nSince 1/13 Avg: ${_.mean(monthAverages).toFixed(2)}%`) | |
monthAverages.map((month, i)=>{ | |
console.log(`${monthNames[i]}: ${monthAverages[i]}%`); | |
}); | |
console.log(`\n\n`); | |
return [today, _.mean(monthAverages), _.mean(daily), _.max(daily), monthAverages] | |
}); | |
let today = byCountry.map(item => item[0]); | |
let monthlyAverage = byCountry.map(item => item[1]); | |
let dailyAverages = byCountry.map(item => item[2]); | |
let maxDaily = _.mean(byCountry.map(item => item[3])); | |
let monthAverages = [0,1,2,3,4].map((month)=>{ | |
let countries = byCountry.map(item => item[4]).map((country)=>{ | |
return country[month]; | |
}) | |
return _.mean(countries); | |
}); | |
console.log(`\n\n`); | |
console.log(`Global average today: ${_.mean(today).toFixed(2)}%`) | |
console.log(`Global average since 1/13: ${_.mean(dailyAverages).toFixed(2)}%`); | |
console.log('Monthly averages since 1/13', monthAverages.map(item => `${item.toFixed(2)}%`).join(' ')); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment