2017-11-22 5 views
1

R에 익숙하지 않습니다. lda를 사용하여 생성 된 그리드의 모든 점을 분류하려고합니다. 훈련 세트는 rmvnorm(n,mean,sigma)을 사용하여 무작위로 생성 된 두 개의 포인트 그룹입니다. `여기서 R에서 lda로 예측 : 경고 메시지 : 'newdata'에는 1600 개의 행이 있지만 발견 된 변수에는 200 개의 행이 있습니다.

# 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 

내 데이터 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

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

내 그리드는 40 * 40 점으로 구성을하는 부분이며, 따라서 myGird 데이터 프레임의 크기가있다 : 여기 내 코드는 1600 개의 행과 2 개의 열. 데이터 프레임 bothGroupsWithLabel은 200 행과 3 열로 구성되며 처음 두 열은 점의 좌표이고 세 번째 열은 레이블에 사용됩니다. 내 문제는 전화 할 때 predict(model.lda, newdata=myGrid)이 경고 메시지가 나타납니다. Warning message: 'newdata' had 1600 rows but variables found have 200 rows 여기에 무엇이 있습니까? 누구든지 날 도와 줄 수 있니?

답변

0

문제는 모델을 생성 한 방식입니다. 수식과 data=...을 사용할 때 변수 이름 만 사용하는 것이 좋습니다. 이 작업을 수행하려면 newdata에서 변수 이름을 일치시켜야합니다. 마지막 몇 줄이 될 수 있도록 다음

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

과 :

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

을 당신이 원하는 것을 얻을해야 있도록 할 때에는 myGrid이 줄을 추가 만듭니다.

+0

고맙습니다. 문제가 해결되었습니다! –