Monday, March 16, 2015

Linear regression

Linear regression

a. Predicts an outcome variable, or dependent variable
b. Predicts using a set of independent variables

The goal of linear regression is to create a predictive line through the data.

One-variable linear regression, which just uses one independent variable to predict the dependent variable.


The best model or best choice of coefficients Beta 0 and Beta 1 has the smallest error terms or smallest residuals.

One measure of the quality of a regression line is the sum of squared errors, or SSE. This is the sum of the squared residuals or error terms.

SSE= Sq (E1) + Sq (E2) + ...Sq (EN)

Although SSE allows us to compare lines on the same data set, it's hard to interpret for two reasons.
a. The first is that it scales with n, the number of data points. If we built the same model with twice as much data, the sum of squared errors might be twice as big.  But this doesn't mean it's a worse model.
b.The second is that the units are hard to understand. Some of squared errors is in squared units
of the dependent variable.

RMSE = Sqrt (SSE/N)

Baseline model, the model that does not use any variables. The baseline model predicts the average value of the dependent variable regardless of the value of the independent variable.

SST = The sum of squared errors for the baseline model is also known as the total sum of squares, commonly referred to as SST.

Sq(R) = 1 - SSE/SST

Multiple linear regression allows you to use multiple variables at once to improve the model.

Adding variable always increases value.



Sunday, March 15, 2015

Plots

1. plot (vectorname1, vectorname2)
2. plot (vectorname1, vectorname2, type="l") - converts plot into line instead of dots
3. plot (vectorname1, vectorname2, type="l", col = "red") - red color
4. lines (vectorname1, vectorname2) - add lines to already existing plot
5. abline (v="vertical line point", lwd="width of line")
6. plot (vectorname1, vectorname2, type="l", col = "red", ylim=c(0,210)) - make the y-axis range from 0 to 210

Tuesday, March 10, 2015

R Date Commands

1. Convert to date

as.Date(strptime(mvt$Date, "%m/%d/%y %H:%M"))

2. Get month from a vector

months(vectorname_date)

3. Get weekdays from a vector

weekdays(vectorname_date)


Sunday, March 8, 2015

R Commands

Opening a CSV File

USDA = read.csv("USDA.csv")

Statistical functions

summary (USDA)

Getting a vector from data frame

dataframe$vectorname

Dataframe operations

dataframename = data.frame(vector1, vector2)
dataframe$newvector = newvector
subset (dataframe, condition) e.g. subset (USDA, sodium > 10000)
rbind(dataframe1, dataframe2)

nrow (dataframe)
names (USDA)
str(USDA)

merge(targetdataframe, sourcedataframe, by.x="fieldname1", by.y="fieldname2", all.x=TRUE)
by.x =
by.y =
all.x=TRUE means we want to keep all rows from the "x" data frame (CPS), even if some of the rows' MetroAreaCode doesn't match any codes in MetroAreaMap

Vector operations
A vector is a series of numbers or characters stored as the same object.

c(2,3,5,8,13) - combine function creates a vector of 5 numbers
which.min (vectorname)
which.max (vectorname)
match (valuetomatch, vectorname)
mean(vectorname, na.rm = TRUE)
sd(vectorname, na.rm = TRUE)
str (vectorname)

Plots
plot(x-axis-vectorname, y-axis-vectorname)
plot(x-axis-vectorname, y-axis-vectorname, xlab = "X Axis Label", ylab = "Y Axis Label", main= "Graph Title", col = "Color of Graph")
hist(vectorname)
hist(vectorname, xlab = "X Axis Label")
hist(vectorname, xlab = "X Axis Label",  main= "Graph Title")
hist(vectorname, xlab = "X Axis Label",  main= "Graph Title", xlim = c(0,100))
hist(vectorname, xlab = "X Axis Label",  main= "Graph Title", xlim = c(0,100), breaks = 2000)
boxplot(vectorname)

Table
table (vectorname)
table (vector1, vector2)
tapply (argument1, argument2, argument3) = Group argument1 by argument2 and apply argument3

e.g. tapply(IBM$StockPrice, months(IBM$Date), mean)
e.g. tapply(is.na(CPS$MetroAreaCode), CPS$State, mean)


Misc
seq (0,100,2)

Missing Values
is.na(vectorname)