Skip to content

Instantly share code, notes, and snippets.

View njtierney's full-sized avatar
🐝
Back at work

Nicholas Tierney njtierney

🐝
Back at work
View GitHub Profile
# turn the data into a correlation matrix
cor(mydata)
# perform the PCA, using `principal`
pca.fit <- principal(mydata,
nfactors = ncol(mydata), # the number of factors = the number of columns in my data
rotate = "varimax", # I want to perform varimax rotation on the factors
residuals = TRUE, # report the residuals
scores = TRUE) # find the component scores
@njtierney
njtierney / alt-gen-MCAR-fun
Last active August 29, 2015 14:07
create MCAR data (from package `mi`).
mi:::.create.missing
function (data, pct.mis = 10)
{
n <- nrow(data)
J <- ncol(data)
if (length(pct.mis) == 1) {
n.mis <- rep((n * (pct.mis/100)), J)
}
else {
if (length(pct.mis) < J)
@njtierney
njtierney / knitr set up
Created March 9, 2015 03:40
This code is my standard setup for rmarkdown in knitr.
```{r global_options, include=FALSE, cache=FALSE}
library(knitr)
# Set basic options. You usually do not want your code, messages, warnings etc.
# to show in your actual manuscript however for the first run or two these will
# be set on.
opts_chunk$set(echo=FALSE,
warning=FALSE,
message=FALSE,
cache = TRUE,
include = FALSE,
Serial study of factors influencing changes in cardiac output during human pregnancy 557 1989
Bayesian inference for a discretely observed stochastic kinetic model 172 2008
159 1992
151 1996
150 1987
127 1992
126 1992
97 1996
95 2003
94 1991
# assuming data has columns named something like: "lat", "long", and "timestamp"
library(ggmaps)
library(ggplot2)
ggplot(data = data,
aes(x = lat,
y = long)) +
geom_point() +
layout title
post
Another test post

OK so here is my idea about How these post snippets could work. could work.

What problem are you addressing.

In this snippet we address the problem: ...

#' meteo_distance
#'
#' @description
#'
#' @param data a dataframe. Expects col headers with names latName and longName
#' @param lat Latitude to centre search at
#' @param long Longitude to centre search at
#' @param latName Name of latitude header name in data, Default = 'latitude'
#' @param longName Name of longitude header name in data. Default = 'longitude'
#' @param units Units of the latitude and longitude values: degrees 'deg', radians 'rad', d/m/s 'dms'. Default = 'deg'
context("Test names with spaces")
messy_names <- data_frame(`Sepal Width` = iris$Sepal.Width,
`Sepal Length` = iris$Sepal.Length,
`Petal Length` = iris$Petal.Length,
`Species Names` = iris$Species)
test_that("vis_dat works on dataframes with irregular variable names", {
expect_success(
vis_dat(messy_names)
@njtierney
njtierney / find_previous_smoker.R
Created May 24, 2016 05:52
smoking data: Has there been a smoker in the past
# calculate if there has been a smoker in the past
# "smoked_previously"
data_prac <- data_frame(ID = c(1, 1, 1, 1,
201, 201,
342, 342, 342,
613, 613, 613, 613, 613),
time = c(1, 2, 3, 4,
1, 2,
@njtierney
njtierney / microbm_visdat_wakefield.md
Created May 29, 2016 14:30
benchmark for vis_dat and table_heat
library(microbenchmark)
library(wakefield)
library(visdat)

mb1 <-
microbenchmark(vis_dat(airquality),
               table_heat(airquality))

boxplot(mb1)