You are on page 1of 3

STAT 101 RStudio Commands Dr. Kari Lock Morgan Based on source("/shared/kari.lock.morgan@gmail.com/Lock5.

R") COMMAND EXAMPLE DESCRIPTION Basic arithmetic == can be read as is equal to not equal to Opens help for a command Combines 1, 4, 5 into one object Loads in a dataset from the textbook Get information about the dataset Extracts a variable from a dataset New data only cases where var < 5 Draw a random sample of 5 from x Reads in data from a google doc1

Basic R Commands + - * / ^ 2*3 == varname == male != ? c varname != male ?plot c(1,4,5)

Data data ? $ subset sample google.doc

data(dataname) ?dataname dataname$varname subset(data, data$var<5) sample(x, 5) google.doc(key)

Click on dataset name in workspace window to view dataset Summary Statistics table table(x) table(x,y) mean mean(y) mean(y~x) median median(y) median(y~x) sd sd(y) percentile summary cor diffMean percentile(y, .05) summary(y) cor(x,y) diffMean(y~x) Make a table for one or two categorical variables Calculate the mean Calculate the median Calculate the standard deviation Calculate a percentile of x Five number summary of x Calculate the correlation Calculate difference in means

The key is everything between key= and # in the link for the google spreadsheet. For this to work on your own google docs, you have to first click File > Publish to Web > Start Publishing.
1

diffProp

diffProp(gender==male~x)

Calculate difference in proportions Create a bar chart, segmented bar chart, or side-by-side bar chart Create a pie chart Create a histogram Create a boxplot Create a scatterplot Adds a title to a graphic Labels the x and y-axes Repeats what is after the * 100 times Draw a random sample of 5 from x with replacement Randomly reorders x Calculate a percentile of y or specified distribution Find the proportion as extreme as stat in dist or specified distribution

Visualization barplot

pie hist boxplot plot main xlab, ylab Inference do sample

barplot(x) barplot(x~y) barplot(x~y, beside=TRUE) pie(table(x)) hist(y) boxplot(y) boxplot(y~x) plot(x,y) hist(y, main=Title) plot(x,y, xlab=x-axis)

do(100)*mean(sample(x,10)) sample(x, 5, replace=TRUE)

shuffle shuffle(x) percentile percentile(y, .05) percentile(normal, .01) percentile(t, .975, df=20) tail.p tail.p(dist, stat, tail=upper)
tail.p(normal, stat, tail=lower) tail.p(t, stat, tail=two, df=20) tail.p(chisquare, stat, df=2, tail=upper) tail.p(f, stat, df1=3,df2=100, tail=upper)

resample resample(x, level=.95) reallocate reallocate(y, x) prop.test


prop.test(c(count1,count2), c(n1,n2))

t.test

t.test(y1, y2) t.test(y~x) summary(aov(y~x))

aov

chisq.test chisq.test(x1,x2)

chisq.test(x1,x2,simulate.p.value=TRUE)

Create a bootstrap CI Conduct a randomization test Conduct a test and interval for difference in proportions Conduct a test and interval for difference in means ANOVA for difference in means Chi-square test for association

Regression lm

lm(y~x, data=dataname) lm(y~x1+x2, data=dataname) summary(model)

summary

$residuals model$residuals predict predict abline


predict(model)
predict(model, interval=confidence) predict(model, interval=prediction)

Fits a model regressing y on explanatory variables x1, x2, Gives model output (model is an object created with lm) Extracts residuals from a model Gives predicted values, with intervals Predicted values for new cases Adds a regression line to a scatterplot

newdata = as.data.frame(cbind(var1 = 10, var2=20)) predict(model, newdata) abline(model)

You might also like