This week we’ll look at some tips and tricks in R, mostly some simple methods to help debug R code that isn’t working.
Lecture: debug.ppt
Lab (borrowed!): interactive debugging tools
This week we’ll look at some tips and tricks in R, mostly some simple methods to help debug R code that isn’t working.
Lecture: debug.ppt
Lab (borrowed!): interactive debugging tools
Problems in social network analysis are similar to those in spatial analysis, except instead of coordinates we just have information about relationships. There are all sorts of measures that can be computed from networks to feed into regular regressions, and there are also ways to make the network structure itself the outcome of interest.
Lecture: SNA Lecture (.ppt)
Lab: SNA Lab
Data: Data
Code: sna.R
This lecture covers regression where each observation is associated with a polygon. Two easy models to try out in R are the spatial lag model and the spatial error model.
Lecture: GSS.ppt
Lab: GSS.doc
Data: nc.RData
Code: gss.R
CRAN Task View on other spatial analysis methods/topics (lists package that support different types of models).
Want to use R as a GIS? It’s easy! It’s possible to use R entirely, but in combination with QGIS and Google Earth you’ll never need to download another ArcGIS license file!
Lecture: LearnRGIS2014
Lab: LearnRGIS
Tired of copy-pasting everything in your paper each time someone asks you to change your model? Let R do the work for you! This week we will learn two ways to write automatic reports using R: R Markdown (.docx) and Sweave (LaTeX .pdf).
Lecture: LearnRReports2014
Want to write an R program that you (and others) can use without re-writing code? It is easy to do in R – just write a graphical user interface! This tutorial will walk you through using the gWidgets package to create an interface that will let you preform analyses by clicking buttons, dragging sliders, selecting from menus, and entering text!
Since most of the school’s rooms are booked for open house, we won’t have a lecture this week. However, I’ll be available in room 201 Rosenau (student study space) for any R questions from 10:30-11:30.
Lab: gui.R
Edit: During the lab today I realized that the GUI toolkit we are using (GTK) can be finicky to install at first. R should pop up a box asking if you want to install GTK when you run the install.packages command from the script. If it dosen’t, here are some things we tried to get it to install correctly:
1. Make sure you have the upper/lower cases right (gWidgetsRGtk and RGtk).
2. Make sure you have the latest version of R itself (from CRAN: http://cran.r-project.org/). The RStudio version shouldn’t matter.
3. Try installing and loading the RGtk2 package first: install.packages("RGtk2")
library(RGtk2)
3. If you are using linux, semi-manually install the gtk development package:
sudo apt-get install libgtk2.0-dev
Youll get lots of Scary Text when you install the package in linux (as usual) but it should finish compiling eventually.
4. Restart R and try again.
Program control can be used to make your program react intelligently to your data, by following branching decision trees. It is also possible to write SAS-style data management code with loops and if statements if you are having a hard time figuring out how to do something with vectors.
Lecture: LearnRProgramControl2014
In the lab we will learn how to create and annotate R’s built-in plots.
Lab Activity: LearnRGraphics
Data files for lab (NHANES III data):
adult.csv
R Script with lab answers:
NHANES script
This week we will practice fitting generalized linear models. These sound complicated but are just extensions of the good old y=mx+b from 9th grade math. Epidemiologists use these models for estimating a variety of effect measures like risk differences, risk ratios, odds ratios, and the like. We won’t talk much about what goes in the model, but we’ll practice plugging our data and model specifications into R.
We’ll also finish up some basic data management from last time.
Lecture: Learn RData Management2014
Lab:GLMs
Data: greensboro.RData
Code:
# point this where your data is.
load("Desktop/greensboro.RData")
library(sp)
pdf("mymap.pdf")
plot(greensboro)
dev.off()
names(greensboro)
p.black <- greensboro$pop.black/greensboro$pop.total
l
ength(greensboro$pop.black)
length(greensboro$distance)/6130
d <- matrix(greensboro$distance,ncol=8)
dim(d)
d.min <- apply(d,1,min)
length(d.min) # looks good
#classify data
close <- 1*(d.min < 5280)
black 0.5)
summary(d.min)
table(close)
mod.linear <- glm(close~black)
summary(mod.linear)
confint(mod.linear)
Getting into access databases is easy from R. Try out the sample code below using this database.
# database.R
# example code for importing access (and other ODBC/DBMS) databases
### Step 1: get the RODBC package set up
install.packages("RODBC") # run once
library(RODBC)
### Step 2: establish a database connection
# option a: manual
setwd("D:/LearnR") # point to where your database is
mydatabase <-odbcConnectAccess2007("mydatabase.accdb")
# option b: select using a menu instead
mydatabase <- odbcDriverConnect()
### Once you have a connection, you can pull tables or submit queries
# Grab a table as a data.frame by name
nutrition <- sqlFetch(mydatabase,"Nutrition")
# Submit sql-like query to database to get more complex tables or vectors
demographics <- sqlQuery(mydatabase,"select * from Demographics")
demographics
jobs <- sqlQuery(mydatabase,"select Job from Demographics")
jobs
# Write a dataframe back as another table
sqlSave(mydatabase, jobs, tablename = "Job Copy", append = TRUE)
close(mydatabase)
Functions are the “vocabulary” of R; after you master the basics most of your work in R will be learning how to use new functions or put together functions you are familiar with in new ways to solve problems!
This week we will talk about several new data types often used in functions, practice some core functions used for getting data into R, load a package, and write a few new functions ourselves.
University Operator: (919) 962-2211 | © 2025 The University of North Carolina at Chapel Hill |