البرمجة

تنبؤ البيانات باستخدام نموذج الانحدار اللوجستي

It looks like the error you’re encountering is due to a mismatch in the number of features between your training and test datasets. The glm() function you used for training the model included the generationMethod variable, but your test dataset Test does not have this variable. This causes the predict() function to fail because it expects the same set of variables in the test dataset as in the training dataset.

To resolve this issue, you can either remove the generationMethod variable from your training dataset before fitting the model, or you can add a placeholder column for generationMethod in your test dataset with a default value (e.g., 0 or NA) to match the structure of the training dataset.

Here’s how you can remove the generationMethod variable from your training dataset:

R
PairsTrain <- PairsTrain[, -which(names(PairsTrain) == "generationMethod")] mod1 <- glm(isDuplicate ~., data = PairsTrain, family = binomial)

And then, add a placeholder column for generationMethod in your test dataset:

R
Test$generationMethod <- 0 # Or any other suitable default value

Now, you should be able to make predictions on your test dataset without encountering the error:

R
PredTest <- predict(mod1, newdata = Test, type = "response")

This approach ensures that the structure of your test dataset matches the structure of your training dataset, allowing you to make predictions successfully.

المزيد من المعلومات

عندما تستخدم دالة glm() في R لتدريب نموذج الانحدار اللوجستي، يجب أن تتأكد من أن جميع المتغيرات التي تم استخدامها في تدريب النموذج متوفرة في بيانات الاختبار. إذا كان أي من المتغيرات غير متوفر في بيانات الاختبار، فستحتاج إلى إما إزالته من النموذج قبل تدريبه، أو تضمين قيم افتراضية لهذه المتغيرات في بيانات الاختبار.

في حالتك، الخطأ الذي تحصل عليه object 'generationMethod' not found يحدث لأن المتغير generationMethod غير موجود في بيانات الاختبار Test. يمكنك حل هذه المشكلة عن طريق إزالة المتغير generationMethod من بيانات التدريب، أو بإضافة عمود فارغ يحتوي على قيم افتراضية لـ generationMethod في بيانات الاختبار.

إذا قمت بإزالة المتغير generationMethod من بيانات التدريب، يمكنك القيام بذلك باستخدام الأمر التالي:

R
PairsTrain <- PairsTrain[, -which(names(PairsTrain) == "generationMethod")] mod1 <- glm(isDuplicate ~., data = PairsTrain, family = binomial)

ثم، يمكنك إضافة عمود فارغ لـ generationMethod في بيانات الاختبار باستخدام الأمر التالي:

R
Test$generationMethod <- 0 # أو أي قيمة افتراضية أخرى

بعد ذلك، يجب أن تكون قادرًا على تنبؤ بيانات الاختبار دون الحصول على الخطأ السابق:

R
PredTest <- predict(mod1, newdata = Test, type = "response")

هذا الإجراء يضمن أن هيكل بيانات الاختبار يتطابق مع هيكل بيانات التدريب، مما يتيح لك القيام بالتنبؤ بنجاح.

مقالات ذات صلة

زر الذهاب إلى الأعلى

هذا المحتوى محمي من النسخ لمشاركته يرجى استعمال أزرار المشاركة السريعة أو تسخ الرابط !!