Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 7bb4b21
  • /
  • generate_modularity_class_fits.R
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:1d25f6dac1c1fd655cb3658a67d470a350b66ca3
directory badge
swh:1:dir:7bb4b21824a7d380a312fe81124de7dfd015b831

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
generate_modularity_class_fits.R
# This file generates the fitted  curves to each of the four identified clusters

##################
# Libraries

library(tidyverse)
library(lubridate)
library(lme4)
library(nlme)
library(broom.mixed)
library(mgcv)
library(arrow)

##################s
# Read in data

clusters <- read_parquet('sinusoidal_fits/fips_modulclass.parquet') %>% 
  select(-X1) %>% 
  rename(fips = node) %>% 
  mutate(fips = as.double(fips))

df.fips <- read_parquet('sinusoidal_fits/state_and_county_fips_master.parquet') %>% 
  mutate(fips = if_else(fips ==02270, 02158, fips),
         fips = if_else(fips == 46113, 46102, fips))

df.full <- read_csv('sinusoidal_fits/indoor_outdoor_ratio_unsmoothed.csv') %>% 
  left_join(df.fips) %>% 
  left_join(clusters) %>% 
  filter(!is.na(fips), !is.na(state), !is.na(week), !is.na(modularity_class)) %>% 
  group_by(fips) %>% 
  arrange(week) %>%
  mutate(t = row_number()) %>% 
  ungroup() %>% 
  mutate(state = as.factor(state))

###########
# Fit sine curve estimates 

params <- df.full %>% 
  nest(data = -modularity_class) %>% 
  mutate(fit = map(data, ~ nls(r_raw ~ A*sin(omega*t+phi)+C, 
                               data=.x, 
                               start=c(A=.25,omega=.127,phi=1,C=.98))),
         tidied = map(fit, tidy),
         preds = map(fit, predict, newdata = tibble(t=1:182)))

###########
# Save sine curve fits

params %>% 
  select(modularity_class, tidied) %>% 
  unnest(tidied) %>% 
  write_csv('data/sine_curve_cluster_fits.csv')

params %>% 
  select(modularity_class, preds) %>% 
  unnest(preds) %>% 
  group_by(modularity_class) %>% 
  mutate(t = row_number()) %>% 
  ungroup() %>% 
  left_join(df.full %>% select(week, t) %>% unique()) %>% 
  write_csv('data/sine_curve_cluster_preds.csv')

#########
# Fit GAM

params <- df.full %>% 
  nest(data = -modularity_class) %>% 
  mutate(fit = map(data, ~ gam(r_raw ~ s(t), 
                               data=.x)),
         tidied = map(fit, tidy),
         preds = map(fit, predict, newdata = tibble(t=1:182)))

#########
# Save GAM estimates

params %>% 
  select(modularity_class, tidied) %>% 
  unnest(tidied) %>% 
  write_csv('./gam_cluster_fits.csv')

params %>% 
  select(modularity_class, preds) %>% 
  unnest(preds) %>% 
  group_by(modularity_class) %>% 
  mutate(t = row_number()) %>% 
  ungroup() %>% 
  left_join(df.full %>% select(week, t) %>% unique()) %>% 
  write_csv('./gam_cluster_preds.csv')

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API