簡體   English   中英

當y是r中的指標矩陣時,如何執行多元線性回歸?

[英]How to perform a multivariate linear regression when y is an indicator matrix in r?

這是我第一次發布問題,希望它看起來不會引起混淆。 非常感謝您的寶貴時間。

我正在研究一個郵政編碼數據集,可以在這里下載: http : //statweb.stanford.edu/~tibs/ElemStatLearn/datasets/zip.train.gz http://statweb.stanford.edu/~tibs/ElemStatLearn /datasets/zip.test.gz

通常,我的目標是使主成分回歸模型與火車數據集中的前3個PC對應,這些響應變量的手寫數字為2、3、5和8,然后使用測試數據進行預測。 我的主要問題是在X矩陣上執行PCA之后,我不確定是否正確執行了回歸部分。 我已將響應變量轉換為2487 * 4指標矩陣,並希望擬合多元線性回歸模型。 但是預測結果不是二項式指標,因此我很困惑如何將預測解釋回原始的響應變量,即預測為2、3、5或8。或者我是否完全做了回歸部分錯誤? 這是我的代碼如下:

首先,我用這些響應變量等於2、3、5和8的方法構建了子集:

zip_train <- read.table(gzfile("zip.train.gz")) 
zip_test <- read.table(gzfile("zip.test.gz"))
train <- data.frame(zip_train)
train_sub <- train[which(train$V1 == 2 | train$V1 == 3 | train$V1 == 5 | train$V1 == 8),]
test <- data.frame(zip_test)
test_sub <- test[which(test$V1 == 2 | test$V1 == 3 | test$V1 == 5 | test$V1 == 8),]    
xtrain <- train_sub[,-1]
xtest <- test_sub[,-1]
ytrain <- train_sub$V1
ytest <- test_sub$V1

其次,我將X矩陣居中,並使用svd計算了前3個主要成分:

cxtrain <- scale(xtrain)
svd.xtrain <- svd(cxtrain)
cxtest <- scale(xtest)
svd.xtest <- svd(cxtest)

utrain.r3 <- svd.xtrain$u[,c(1:3)] # this is the u_r
vtrain.r3 <- svd.xtrain$v[,c(1:3)] # this is the v_r
dtrain.r3 <- svd.xtrain$d[c(1:3)]
Dtrain.r3 <- diag(x=dtrain.r3,ncol=3,nrow=3) # creat the diagonal matrix D with r=3
ztrain.r3 <- cxtrain %*% vtrain.r3 # this is the scores, the new components

utest.r3 <- svd.xtest$u[,c(1:3)] 
vtest.r3 <- svd.xtest$v[,c(1:3)] 
dtest.r3 <- svd.xtest$d[c(1:3)]
Dtest.r3 <- diag(x=dtest.r3,ncol=3,nrow=3) 
ztest.r3 <- cxtest %*% vtest.r3 

第三,這是我不確定是否以正確的方式進行操作的部分,我將響應變量轉換為指標矩陣,並執行了如下的多元線性回歸:

ytrain.ind <-cbind(I(ytrain==2)*1,I(ytrain==3)*1,I(ytrain==5)*1,I(ytrain==8)*1)
ytest.ind <- cbind(I(ytest==2)*1,I(ytest==3)*1,I(ytest==5)*1,I(ytest==8)*1)

mydata <- data.frame(cbind(ztrain.r3,ytrain.ind))
model_train <- lm(cbind(X4,X5,X6,X7)~X1+X2+X3,data=mydata)
new <- data.frame(ztest.r3)
pred <- predict(model_train,newdata=new)

但是,該pred並不是指標矩陣,因此我迷失了如何將它們解釋回數字並將它們與真實測試數據進行比較以進一步計算預測誤差。

我終於想出了如何使用類別y進行多元線性回歸。 首先,我們需要將y轉換為指標矩陣,然后才能將矩陣中的0和1解釋為概率。 然后對x進行y回歸以建立線性模型,最后使用該線性模型對x的測試集進行預測。 結果是一個尺寸與我們的指標矩陣相同的矩陣。 並且所有條目也應解釋為概率,盡管它們可能大於1或小於0(這就是為什么以前讓我感到困惑的原因)。 因此,我們需要找到每行的最大數目,以查看哪個預測的y具有最高的概率,而這個y將是我們的最終預測。 這樣,我們可以將連續數字轉換回類別,然后創建一個表與y的測試集進行比較。 所以我更新了我以前的代碼,如下所示。

首先,我用這些響應變量等於2、3、5和8來構建子集(代碼與我在問題中發布的代碼相同):

zip_train <- read.table(gzfile("zip.train.gz")) 
zip_test <- read.table(gzfile("zip.test.gz"))
train <- data.frame(zip_train)
train_sub <- train[which(train$V1 == 2 | train$V1 == 3 | train$V1 == 5 | train$V1 == 8),]
test <- data.frame(zip_test)
test_sub <- test[which(test$V1 == 2 | test$V1 == 3 | test$V1 == 5 | test$V1 == 8),]    
xtrain <- train_sub[,-1]
xtest <- test_sub[,-1]
ytrain <- train_sub$V1
ytest <- test_sub$V1

其次,我將X矩陣居中,並使用eigen()計算了前3個主要成分。 我更新了這部分代碼,因為我對x進行了標准化,而不是將其居中放置在先前的代碼中,從而導致x的協方差矩陣和cov(x)的特征向量的計算錯誤。

cxtrain <- scale(xtrain, center = TRUE, scale = FALSE) 
eigenxtrain <- eigen(t(cxtrain) %*% cxtrain / (nrow(cxtrain) -1)) # same as get eigen(cov(xtrain)), because I have already centered x before
cxtest <- scale(xtest, center = TRUE, scale = FALSE)
eigenxtest <- eigen(t(cxtest) %*% cxtest/ (nrow(cxtest) -1))
r=3 # set r=3 to get top 3 principles
vtrain <- eigenxtrain$vectors[,c(1:r)] 
ztrain <- scale(xtrain) %*% vtrain # this is the scores, the new componenets
vtest <- eigenxtrain$vectors[,c(1:r)] 
ztest <- scale(xtest) %*% vtest

第三,我將響應變量轉化為指標矩陣,並對訓練集進行了多元線性回歸。 然后使用此線性模型進行預測。

ytrain.ind <- cbind(I(ytrain==2)*1,I(ytrain==3)*1,I(ytrain==5)*1,I(ytrain==8)*1)
ytest.ind <- cbind(I(ytest==2)*1,I(ytest==3)*1,I(ytest==5)*1,I(ytest==8)*1)

mydata <- data.frame(cbind(ztrain,ytrain.ind))
model_train <- lm(cbind(X4,X5,X6,X7)~X1+X2+X3,data=mydata)
new <- data.frame(ztest)
pred<- predict(model_train,newdata=new)

pred是一個包含所有概率條目的矩陣,因此我們需要將其轉換回類別y的列表。

pred.ind <- matrix(rep(0,690*4),nrow=690,ncol=4) # build a matrix with the same dimensions as pred, and all the entries are 0.
for (i in 1:690){
  j=which.max(pred[i,]) # j is the column number of the highest probability per row
  pred.ind[i,j]=1 # we set 1 to the columns with highest probability per row, in this way, we could turn our pred matrix back into an indicator matrix
}

pred.col1=as.matrix(pred.ind[,1]*2) # first column are those predicted as digit 2
pred.col2=as.matrix(pred.ind[,2]*3)
pred.col3=as.matrix(pred.ind[,3]*5)
pred.col4=as.matrix(pred.ind[,4]*8)
pred.col5 <- cbind(pred.col1,pred.col2,pred.col3,pred.col4) 

pred.list <- NULL
for (i in 1:690){
  pred.list[i]=max(pred.col5[i,])
} # In this way, we could finally get a list with categorical y

tt=table(pred.list,ytest)
err=(sum(tt)-sum(diag(tt)))/sum(tt) # error rate was 0.3289855

對於第三部分,我們也可以執行多項式邏輯回歸。 但是通過這種方式,我們不需要將y轉換為指標矩陣,只需將其分解。 因此,代碼如下所示:

library(nnet)
trainmodel <- data.frame(cbind(ztrain, ytrain))
mul <- multinom(factor(ytrain) ~., data=trainmodel) 
new <- as.matrix(ztest)
colnames(new) <- colnames(trainmodel)[1:r]
predict<- predict(mul,new)
tt=table(predict,ytest)
err=(sum(tt)-sum(diag(tt)))/sum(tt) # error rate was 0.2627907

因此,它表明邏輯模型的性能確實優於線性模型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM