Monday, October 18, 2010

R: Linear Discriminant Analysis

lib(s): MASS, klaR

# Data input: generic

# Before using this script:
library(MASS)
library(klaR)

# - Using Jacknifed Prediction - wtf is jacknifed prediction???

fit <- lda(infile$dep.cat.var ~ indep.var1 + indep.var2 + indep.var3 + ... + indep.var, data=infile, na.action="na.omit") # this leaves out NA values. Don't know why if you add "CV=TRUE" at the back, it doesn't work - WTF?
fit # show results

# visualize the results with a graph.
plot(fit)

# another visual - not beta tested... it says figure margins too large (whatever that means)
plot(fit, dimen=1, type="both") # fit from lda

# Assess the accuracy of the prediction
# percent correct for each group in you dependent categorical variable
ct <- table(infile$dep.cat.var, fit$class)
diag(prop.table(ct, 1))
# total percent correct
sum(diag(prop.table(ct)))

# an exploratory graph
partimat(infile$dep.cat.var ~ indep.var1 + indep.var2 + indep.var3 + ... + indep.var, data=infile, method="lda")

# /* END OF LINEAR DISCRIMINANT ANALYSIS */

No comments:

Post a Comment