2017-11-22 1 views
1

Ich bin neu in R. Ich versuche, lda zu verwenden, um alle Punkte in einem generierten Gitter zu klassifizieren. Der Trainingssatz besteht aus zwei zufällig erzeugten Punktgruppen unter Verwendung von rmvnorm(n,mean,sigma). Hier ist mein Code: `Vorhersage mit lda in R: Warnmeldung: 'newdata' hatte 1600 Zeilen, aber gefundene Variablen haben 200 Zeilen

# number of samples 
n=100; 

# parameters: G2 
meanG1 = matrix( 
    c(2, 2), # the data elements 
    nrow=1,    # number of rows 
    ncol=2,    # number of columns 
    byrow = TRUE)  # fill matrix by rows 
sigmaG1 = matrix( 
    c(1,0,0,1), # the data elements 
    nrow=2,    # number of rows 
    ncol=2,    # number of columns 
    byrow = TRUE)  # fill matrix by rows 

library(mvtnorm) 

# Generating a matrix G1 with norm distribution 
G1 = rmvnorm(n, meanG1, sigmaG1) 
G1[,3]=1 

# parameters: G2 
meanG2 = matrix( 
    c(0, 0), # the data elements 
    nrow=1,    # number of rows 
    ncol=2,    # number of columns 
    byrow = TRUE)  # fill matrix by rows 
sigmaG2 = matrix( 
    c(1,0.75,0.75,1), # the data elements 
    nrow=2,    # number of rows 
    ncol=2,    # number of columns 
    byrow = TRUE)  # fill matrix by rows 

# # Generating a matrix G2 with norm distribution 
G2 = rmvnorm(n, meanG2, sigmaG2) 

# adding a column as a label = 1 to G1 matrix 
G1 = cbind(G1, 1) 
# adding a column as a label = 2 to G2 matrix 
G2 = cbind(G2, 2)  
# Concatenate both matrices 
G = rbind(G1,G2)  
# Transforming Matrix into dataFrame 
bothGroupsWithLabel <- as.data.frame(G) 
# Shuffling data row-wise 
bothGroupsWithLabel <- bothGroupsWithLabel[sample(nrow(bothGroupsWithLabel)),] 

# plotting the generated matrices 
plot(c(G1[,1]),c(G1[,2]),col="red") 
points(c(G2[,1]),c(G2[,2]),col="blue") 

# Generating a grid 
K = 40; 
seqx1 = seq(min(G1[,1]),max(G1[,1]),length = K) 
seqx2 = seq(min(G1[,2]),max(G1[,2]),length = K) 
myGrid = expand.grid(z1=seqx1,z2=seqx2); 

plot(myGrid[,1],myGrid[,2]) 

library(MASS) 

# Creating a model 
model.lda = lda(bothGroupsWithLabel[,3] ~bothGroupsWithLabel[,1]+bothGroupsWithLabel[,2] , data = bothGroupsWithLabel); 
Ypred = predict(model.lda, newdata=myGrid); 
Ypredgrid = Ypred$class 

Hier ist ein Teil meiner Daten bothGroupsWithLabel V1 V2 V3 69 2.0683949 0.5779272 1 53 2.1261046 2.0420350 1 118 -1.4502033 -1.4775360 2 148 1.1705251 1.5437296 2 195 0.3100763 -0.2594026 2 40 1.8573633 3.7717020 1

und myGrid z1 z2 1 0.1048024 -0.2034172 2 0.2227540 -0.2034172 3 0.3407055 -0.2034172 4 0.4586571 -0.2034172 5 0.5766086 -0.2034172 6 0.6945602 -0.2034172

mein Raster besteht aus 40 * 40 Punkte, damit die Größe des myGird Datenrahmen 1600 Reihen und 2 Spalten. Der Datenrahmen bothGroupsWithLabel besteht aus 200 Zeilen und 3 Spalten, die ersten beiden Spalten sind die Koordinaten der Punkte und die dritte Spalte wird für Beschriftungen verwendet. Mein Problem ist, wenn ich rufe predict(model.lda, newdata=myGrid) Ich bekomme diese Warnmeldung: Warning message: 'newdata' had 1600 rows but variables found have 200 rows Was fehlt mir hier? Kann mir bitte jemand helfen?

Antwort

0

Das Problem ist die Art, wie Sie Ihr Modell erstellt haben. Bei Verwendung einer Formel und data=... ist es besser, nur die Variablennamen zu verwenden. Damit dies funktioniert, müssen Sie auch die Variablennamen in newdata übereinstimmen. Also, wenn Sie erstellen myGrid die Zeile:

names(myGrid) = c("V1", "V2") 

und dann letzten Zeilen machen sein:

model.lda = lda(V3 ~ V1 + V2 , data = bothGroupsWithLabel); 
Ypred = predict(model.lda, newdata=myGrid); 
Ypredgrid = Ypred$class 

dass bekommen sollten, was Sie wollen.

+0

Vielen Dank! Das Problem ist gelöst! –

Verwandte Themen