pointsx3 Xarbindx1x2x3ind clsXa1 XXa 1

# Pointsx3 xarbindx1x2x3ind clsxa1 xxa 1

• Notes
• 6

This preview shows page 2 - 5 out of 6 pages.

#points(x3) ind=sample(seq(1,1500,by=1),N,replace=FALSE) Xa=rbind(x1,x2,x3)[ind,] cls=Xa[,1] X=Xa[,-1] clss=sort(unique(cls)) K=length(clss) pi=rep(NA,K) for(k in 1:K) { pi[k]=sum(cls==clss[k])/N } pi #X=sample(rbind(x1,x2,x3),N,replace=FALSE) p=dim(X)[2] Mu=matrix(NA,K,p) for(k in 1:K) { i=cls==clss[k] Mu[k,]=colMeans(X[i,]) } Q.hat=matrix(0,p,p) for (k in 1:K) { i=cls==clss[k] n=sum(i) term=X[i,]-t(replicate(n,Mu[k,])) Q.hat=Q.hat+t(term)%*%term } Q.hat=Q.hat/(N-K) Q.hat Q.inv=solve(Q.hat) delta=matrix(NA,N,K) for (k in 1:K) { m=Mu[k,] d=log(pi[k])-.5*m%*%Q.inv%*%m for(i in 1:N) { delta[i,k]=d+m%*%Q.inv%*%X[i,] } } my.class=apply(delta,1,which.max) plot(X,col=my.class) plot(X,col=cls) Y=data.frame(P1=Xa[,2],P2=Xa[,3]) hft=lda(cls~P1+P2,data=Y) hft.class=predict(hft,Y)\$class plot(Y,col=hft.class)

Subscribe to view the full document.

#LDA Lab lda.fit=lda(Direction~Lag1+Lag2,data=Smarket,subset=train) lda.fit plot(lda.fit) lda.pred=predict(lda.fit,Smarket.2005) names(lda.pred) lda.class=lda.pred\$class table(lda.class,Direction.2005) mean(lda.class==Direction.2005) sum(lda.pred\$posterior[,1]>=.5) sum(lda.pred\$posterior[,1]<.5) lda.pred\$posterior[1:20,1] lda.class[1:20] sum(lda.pred\$posterior[,1]>.9) #QDA multivariate example N=500 sigma1=matrix(c(1,0,.1,(1-1/100)),2,2) mus1=matrix(c(-.5,-.5),2,1) sigma2=matrix(c(1,0,.25,(1-1/16)),2,2) mus2=matrix(c(0,-.1),2,1) sigma3=matrix(c(1,0,0,1),2,2) mus3=matrix(c(.5,.5),2,1) x1=mvrnorm(N,mus1,sigma1) x1=cbind(1,x1) x2=mvrnorm(N,mus2,sigma2) x2=cbind(2,x2) x3=mvrnorm(N,mus3,sigma3) x3=cbind(3,x3) #plot(x1,x2) #points(x3) ind=sample(seq(1,1500,by=1),N,replace=FALSE) Xa=rbind(x1,x2,x3)[ind,] cls=Xa[,1] X=Xa[,-1] clss=sort(unique(cls)) K=length(clss) pi=rep(NA,K) for(k in 1:K) { pi[k]=sum(cls==clss[k])/N } pi p=dim(X)[2] Mu=matrix(NA,K,p) for(k in 1:K) { i=cls==clss[k] Mu[k,]=colMeans(X[i,]) } Q.hat=array(0,dim=c(p,p,K)) for (k in 1:K) { i=cls==clss[k]
n=sum(i) term=X[i,]-t(replicate(n,Mu[k,])) Q.hat[,,k]=t(term)%*%term/(n-1)

Subscribe to view the full document.

• Fall '16
• alec schimdt

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern