对R

时间:2018-09-02 17:49:12

标签: r

我正在尝试用葡萄牙语复制该pdf的示例5中有关判别分析的内容。在不使用任何软件包的情况下进行帐户处理时,按照下面的代码,我得到的文件结果相同。但是,在使用MASS软件的R软件包和lda ()功能时,帐户不匹配。使用此软件包的功能时出现什么错误?

    rm(list = ls())
    cat("\014")
    RacaA <- data.frame(x1=c(6.36, 5.92, 5.92,6.44,6.40,6.56,6.64,6.68,6.72,6.76,6.72),
    x2=c(5.24,5.12,5.36,5.64,5.16,5.56,5.36,4.96,5.48,5.60,5.08))
    n1 <- length(RacaA$x1)

    RacaB <- data.frame(x1=c(6.00,5.60,5.64,5.76,5.96,5.72,5.64,5.44,5.04,4.56,5.48,5.76),
    x2=c(4.88,4.64,4.96,4.80,5.08,5.04,4.96,4.88,4.44,4.04,4.20,4.80))
    n2 <- length(RacaB$x1)

    barx1A <- mean(RacaA$x1)
    barx2A <- mean(RacaA$x2)
    v1 <- rbind(barx1A,barx2A)
    CovRA <- cov(RacaA)
    barx1B <- mean(RacaB$x1)
    barx2B <- mean(RacaB$x2)
    v2 <- rbind(barx1B,barx2B)
    CovRB <- cov(RacaB)

    Sc <- ((n1-1)/((n1-1)+(n2-1)))*CovRA+((n2-1)/((n1-1)+(n2-1)))*CovRB
    Scinv <- solve(Sc)
    x1_x2 <- cbind(v1-v2)
    Dx <- t(x1_x2)%*%Scinv

    DxA <- Dx%*%v1
    DxB <- Dx%*%v2

    m <- (1/2)*(DxA+DxB)
    m

    library(MASS)

    m.X <- rbind(RacaA,RacaB)
    RA = rep("RacaA", 11)
    RB = rep("RacaB", 12)
    Raca <- c(RA,RB)
    m.Raca <- data.frame(m.X,Raca)

    result.ad <- lda(Raca~., m.Raca)
    pred<-predict(result.ad)$class
    data.pred <- Raca
    table(data.pred,pred)
    plot(result.ad)
    result.ad

0 个答案:

没有答案