Skip to content

Instantly share code, notes, and snippets.

View jfaganUK's full-sized avatar

Jesse Fagan jfaganUK

  • University of Exeter
View GitHub Profile
## Animate fitting ordinary least squares
# Author: Jesse Michael Fagan, PhD
# Date: 2020-02-10T19:29
## Packages ########################################
rm(list=ls())
gc()
library(magick)
library(gganimate)
library(transformr)
@jfaganUK
jfaganUK / animate_least_squares.R
Created October 17, 2019 13:44
Animating the optimization of least squared linear model
## Animate optim ordinary least squares
rm(list=ls())
gc()
library(ggplot2)
library(tidyverse)
library(foreign)
library(gganimate)
library(transformr)
library(data.table)
reduce.tdm <- function(m, rs = 1, cs = 100) {
repeat {
i <- sum(dim(m))
cat("Min row sum\t", min(rowSums(m)),"\tMin col sum\t",min(colSums(m)), "\n")
m <- m[rowSums(m) >= rs, colSums(m) >= cs]
if(sum(dim(m)) == i) break
}
return(m)
}
m <- reduce.tdm(m)
@jfaganUK
jfaganUK / clean_edgelists.R
Created October 23, 2017 19:46
Lists of iGraph Networks
library(dplyr)
library(igraph)
clean_graph <- function(teamID, el) {
e <- el %>% filter(id == teamID) %>%
select(source, target)
g <- graph_from_data_frame(e) %>%
as.undirected(mode = 'collapse', edge.attr.comb = 'max') %>%
simplify
return(g)
@jfaganUK
jfaganUK / uclassify_functions.r
Created March 1, 2017 20:09
Functions for connecting to and running analyses on uClassify
### Functions for connecting and running uclassify text
#' Create an output dataset
#'
#' @param text_vec The vector of text that will be used
#' @return A data.table
uclassify_output_dt <- function(text_vec) {
N <- length(text_vec)
data.table(txt = text_vec, text_coverage = numeric(N))
}
@jfaganUK
jfaganUK / bustracker.r
Created April 30, 2015 16:34
Bus tracker
library(XML)
library(data.table)
library(ggplot2)
library(ggmap)
args <- commandArgs(trailingOnly = T)
cat(args)
x <- readLines('http://realtime.lextran.com/InfoPoint/map/GetVehicleXml.ashx?RouteId=3')
@jfaganUK
jfaganUK / analyze.all.networks.r
Created January 18, 2015 21:17
analyze lots of networks
library(data.table)
library(igraph)
x <- fread("all_networks_140923.csv")
igraph.options(vertex.label = NA, edge.arrow.size = 0, vertex.size = 5)
g <- graph.data.frame(x[,list(source, target, weight, type, survey)])
@jfaganUK
jfaganUK / sqlite_blob.r
Created January 12, 2015 18:26
Storing R Objects as a SQLite Blob
#' ## Storing R Objects in a SQLite Database
#' Two packages we are using. The first is the ```RSQLite``` which will be used to create and manage an in-memory SQLite database. The second is ```igraph``` which I will use to create and visualize a random network. Some of the work I do is on network simulation. I often don't know the metrics I need from a simulated network when it's created, so I want to be able to store the networks that are created so that I can go back later and analyze them.
library(RSQLite)
library(igraph)
#' Create a database in memory.
con <- dbConnect(SQLite(), ":memory:")
#' The table has two columns, an *id* column and a column called *graph* which is a **blob** type. This type just stores binary data.
@jfaganUK
jfaganUK / fastTwoModeProject.r
Created December 30, 2014 16:47
A fast method of getting the two-mode projection of an edgelist network.
twoModeProject <- function(w, m=1, ...) {
if(!require(parallel)) {
stop("Requires the parallel package.")
}
if(!require(data.table)) {
stop("Requires the data.table package.")
}
if(!("data.table" %in% class(w))) {
w <- data.table(w)
@jfaganUK
jfaganUK / sqldf.ran.split.r
Created October 15, 2014 23:53
sqldf random filtering
library(sqldf)
# there is a default dataset called iris that is always loaded into the R scope
# let's say I want to break it into two randomly using sqldf
# assign id's to the rows
my.iris <- iris
my.iris$id <- 1:nrow(my.iris)
ran.split <- data.frame(rs = sample(1:max(my.iris$id), floor(max(my.iris$id) / 2), replace=F))
sqldf("select * from `my.iris` as x inner join `ran.split` as y on x.id = y.rs")