R语言 缺失值NaN

bogh5gae  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(332)

请帮助我,有一个错误,当我试图使输出与测试数据10%(训练数据90)与1000倍的模拟。

#pemanggilan paket yang digunakan
library(caret)
library(KernelKnn)

#perhitungan akurasi prediksi dengan 100 kali simulasi
sim = function(B, p1, k1)
{
  hasileu = matrix(0, B, 4)
  for (i in 1:B)
  {
    #memanggil data boston
    boston = MASS::Boston

    #membagi data train dan data test
    indexes = createDataPartition(boston$medv, p = p1, list = F)
    train = boston[indexes, ]
    test = boston[-indexes, ]
    
    train_x = train[, -14]
    train_x = scale(train_x)[,]
    train_y = train[,14]
    
    test_x = test[, -14]
    test_x = scale(test[,-14])[,]
    test_y = test[,14]
    
    #model prediksi regresi KNN
    pred1 = KernelKnn(train_x, TEST_data = test_x, train_y, k = k1, method = 'euclidean', weights_function = NULL, regression = T)

    #perhitungan akurasi
    mse = mean((test_y - pred1)^2)
    mae = mean(abs(test_y - pred1))
    rmse = sqrt(mse)
    mape = mean(abs((test_y - pred1)/test_y))
    r2 = cor(test_y, pred1)^2
    hasileu[i,1] = rmse
    hasileu[i,2] = mape
    hasileu[i,3] = mae
    hasileu[i,4] = r2
  }
  return(apply(hasileu, 2, mean))
}

#penentuan rentang nilai K
hitung.variasi.k = function(B,p,K)
{
  has11 = matrix(0,K,4)
  for (i in 1:K)
  {
    has11[i,] = sim(B,p,i)
  }
  has11
}

#hasil akurasi berbagai proporsi data uji
K = 10
has11 = hitung.variasi.k(1000, 0.9,K)

Error in KernelKnn(train_x, TEST_data = test_x, train_y, k = k1, method = "euclidean", : 
the TEST_data includes missing values
blmhpbnm

blmhpbnm1#

下面的代码使用tryCatch捕获“error”并继续执行到最后。

library(caret) 
library(KernelKnn)

#perhitungan akurasi prediksi dengan 100 kali simulasi
sim = function(B, p1, k1)
{
  hasileu = matrix(0, B, 4)
  for (i in 1:B)
  {
    #memanggil data boston
    boston = MASS::Boston
    
    #membagi data train dan data test
    indexes = createDataPartition(boston$medv, p = p1, list = F)
    train = boston[indexes, ]
    test = boston[-indexes, ]
    
    train_x = train[, -14]
    train_x = scale(train_x)[,]
    train_y = train[,14]
    
    test_x = test[, -14]
    test_x = scale(test[,-14])[,]
    test_y = test[,14]
    
    errorFlag=0
    #model prediksi regresi KNN
    pred1 =tryCatch(
     KernelKnn(train_x, TEST_data = test_x, train_y, k = k1, method = 'euclidean', weights_function = NULL, regression = T)
    , error=function(e) 
      { print(e); print(paste("error at loop", i));errorFlag<<-1;}
    )

    if (errorFlag==0)
    {
      #perhitungan akurasi
      mse = mean((test_y - pred1)^2)
      mae = mean(abs(test_y - pred1))
      rmse = sqrt(mse)
      mape = mean(abs((test_y - pred1)/test_y))
      r2 = cor(test_y, pred1)^2
      hasileu[i,1] = rmse
      hasileu[i,2] = mape
      hasileu[i,3] = mae
      hasileu[i,4] = r2
    }
  }
  return(apply(hasileu, 2, mean))
}

#penentuan rentang nilai K
hitung.variasi.k = function(B,p,K)
{
  has11 = matrix(0,K,4)
  for (i in 1:K)
  {
    has11[i,] = sim(B,p1=p,k1=i)
  }
  return(has11)
}

#hasil akurasi berbagai proporsi data uji
K = 10
has11 = hitung.variasi.k(B=1000, p=0.9,K=10)

相关问题