Skip to content

Instantly share code, notes, and snippets.

@grantbrown
grantbrown / NormalDemonstration.R
Last active December 18, 2015 02:09
Plots a normal curve and shaded region.
plotNormal = function(lb, ub, mu = 0, sigma = 1)
{
# Generate X an Y pairs for normal curve
X = seq(mu - 4*sigma, mu + 4*sigma, 0.1)
Y = dnorm(X, mean = mu, sd = sigma)
# Plot the normal curve line, lablel axes and plot
plot(X,Y, type = "l", main = "Normal PDF", xlab = "x", ylab = "Density(x)")
# Calculate visible bounds (no use plotting tails that are off the screen)
lb2 = min(max(lb, mu-4*sigma), min(ub, mu+4*sigma))
ub2 = max(max(lb, mu-4*sigma), min(ub, mu+4*sigma))
@grantbrown
grantbrown / BMI.txt
Created June 4, 2013 20:10
BMI vs. % Calories from Fat Data Set
BMI FatPct
27.2 34.6
23.7 39.5
24.9 41.3
32.2 33.3
36.8 43
20 32.3
29.5 31.5
29.3 36.5
44.6 41.3
@grantbrown
grantbrown / tDistributionExample.R
Created June 4, 2013 22:08
Animated illustration of how degrees of freedom affect the shape of the student's-t distribution.
# T Distribution Illustration
TimeDelay = 0.5
plotT = function(df, add = FALSE)
{
f = function(x)
{
return(dt(x, df))
}
curve(f, xlim = c(-6,6), ylim = c(0, 0.5), add = add, main = "T Distribution vs. Standard Normal",ylab = "Density")
@grantbrown
grantbrown / ConfidencePlot.R
Last active May 17, 2016 18:54
Confidence Band Plot - R Function
# Functions to make SLR Confidence and Prediction Band Plots.
# Grant Brown, June 2013
# 171-162
# Internal function used by ConfidenceBandPlot and PredictionBandPlot
BasicRegressionPlot = function(X,Y, main = "", xlab = "",ylab = "", plt = TRUE, matchScale = FALSE)
{
@grantbrown
grantbrown / HeartDiseaseDiabetes.csv
Created June 16, 2013 02:14
Heart Disease and Diabetes CSV File
county HeartCrude HeartAgeAdj DiabetesCrude DiabetesAgeAdj
adair 441.6 211.3 28.7 14.6
adams 352.9 174.4 29 24.5
audubon 318.9 149.9 32.2 17
clarke 389.2 261.6 39.6 31
davis 252.7 172.7 53.8 36.8
decatur 301.2 202.1 28.7 19.2
fremont 289.1 169.1 42.4 25.2
greene 351.2 177 18.7 11.4
howard 379.4 197 25.2 14.9
@grantbrown
grantbrown / ConfidenceBandExample.R
Created June 16, 2013 02:47
Example code and comments for creating a confidence interval at a particular X0 for the mean of Y in a simple linear regression problem.
# To illustrate the use of confidence bands, let's just make up
# a data example. First, to ensure that everyone's simulated data
# looks the same in case you want to work together, let's tell R
# to use the same starting point for random number generation.
# (don't worry about how this works)
set.seed(12345)
@grantbrown
grantbrown / Homework3Template.R
Created June 16, 2013 02:48
R code template for working on homework 3
# Homework 3 R code template
# Before doing anything else, get a copy of HeartDiseaseDiabetes.csv
# This file is available on the github site, and an excel version is
# available on icon. Remember that to use the excel version, you need
# to open it in excel or libre office and save a copy as a csv.
# Once you have obtained a copy of the data set in csv format, take
# note of where you've saved it. We need to tell R to use that folder
@grantbrown
grantbrown / IQ_DI.txt
Created June 16, 2013 16:35
IQ by Delinquency Index Data Set for Section 3
IQ DI
73 38
82 43
84 36
88 34
89 30
91 23
98 26
98 32
101 19
@grantbrown
grantbrown / Homework5.csv
Created June 20, 2013 17:08
Data for homework 5
County AllCancerMortalityCrude PctFarmCorn GraduationRate HospitalBeds NursingFacilities CCDI_Facilities
adair 308.3 27.8 0.97 25 3 1
adams 275.5 24.2 0.947368421 22 1 0
allamakee 244.2 8.1 0.961538462 25 4 1
appanoose 299.1 14 0.782258065 45 2 0
audubon 335 37.3 0.961538462 25 2 2
benton 185.9 40.1 0.95 25 3 1
black.hawk 210.4 40.3 0.901639344 675 11 1
boone 238.5 40.3 0.921259843 42 4 2
bremer 187.3 35.6 0.91503268 50 4 1
@grantbrown
grantbrown / Homework5Template.R
Created June 20, 2013 22:04
Brief template for Homework 5 code
# Change my working directory to wherever the data file is stored
setwd("/home/grantbrown/dev/171-162-Materials/Section4/Homework5/")
CancerData = read.csv("Homework5.csv", head = TRUE)
# Perform the regression:
MyMLR = lm(AllCancerMortalityCrude ~ GraduationRate + HospitalBeds + PctFarmCorn, data = CancerData)
# Take a look at the regression summary: