逻辑回归-eval(family $ initialize):y值必须为0 <= y <= 1

时间:2018-12-27 09:44:54

标签: r logistic-regression

我正在尝试在此处提供的数据集中使用R执行逻辑回归:http://archive.ics.uci.edu/ml/machine-learning-databases/00451/ 这与乳腺癌有关。该数据集包含 Classification 列,其中仅包含 1 (如果患者没有癌症)或 2 (如果患者患有癌症)

library(ISLR)
dataCancer <- read.csv("~/Desktop/Isep/Machine 
Leaning/TD/Project_Cancer/dataR2.csv")
attach(dataCancer)
names(dataCancer)
summary(dataCancer)

cor(dataCancer[,-11])
pairs(dataCancer[,-11])

#Step : Split data into training and testing data
training = (BMI>25)
testing = !training
training_data = dataCancer[training,]
testing_data = dataCancer[testing,]

Classification_testing = Classification[testing]

#Step : Fit a logistic regression model using training data
as.factor(dataCancer$Classification)
classification_model = glm(Classification ~ ., data = 
training_data,family = binomial )
summary(classification_model)

运行脚本时,我得到:

> classification_model = glm(Classification ~ ., data = training_data,family = binomial )
Error in eval(family$initialize) : y values must be 0 <= y <= 1
> summary(classification_model)
Error in summary(classification_model) : object 'classification_model' not found . 

我在其他帖子中添加了 as.factor(dataCancer $ Classification),但它没有解决我的问题。 如果这是该预测变量的内容,您能否建议我将分类的值设置为0到1之间的方法? 感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您在脚本中添加了import UIKit import CoreData class ViewController: UIViewController { let fetchResultController = CoreDataStack.shared.fetchedResultsController @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() print(NSTemporaryDirectory()) title = "List" tableView.dataSource = self } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) CoreDataStack.shared.fetchedResultsController.delegate = self try! CoreDataStack.shared.fetchedResultsController.performFetch() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func addItems(_ sender: UIBarButtonItem) { let alert = UIAlertController(title: "Enter The Item Name", message: nil, preferredStyle: .alert) alert.addTextField(configurationHandler: nil) alert .addTextField { (passwordTextField) in let placeholder = NSAttributedString(string: "Password", attributes: nil) passwordTextField.attributedPlaceholder = placeholder } let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) let save = UIAlertAction(title: "Save", style:.default) { [unowned self] (action) in guard let nameTextfield = alert.textFields?.first, let name = nameTextfield.text else { return } guard let passwordTextfield = alert.textFields?[1], let password = passwordTextfield.text else { return } let entity = NSEntityDescription.entity(forEntityName: "UserEntity", in: CoreDataStack.shared.context) let userEntity = UserEntity(entity: entity!, insertInto: CoreDataStack.shared.context) userEntity.name = name userEntity.password = password try? CoreDataStack.shared.context.save() self.tableView.reloadData() } alert.addAction(cancel) alert.addAction(save) self.present(alert, animated: true, completion: nil) } } extension ViewController: UITableViewDataSource { func numberOfSections(in tableView: UITableView) -> Int { guard let sectionCount = CoreDataStack.shared.fetchedResultsController.sections?.count else { return 0 } return sectionCount } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { guard let sections = CoreDataStack.shared.fetchedResultsController.sections else { return 0 } return sections[section].numberOfObjects } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "DataCell", for: indexPath) let userEntity = CoreDataStack.shared.fetchedResultsController.object(at: indexPath) cell.textLabel?.text = userEntity.name cell.detailTextLabel?.text = userEntity.password return cell } } extension ViewController: NSFetchedResultsControllerDelegate { func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { tableView.beginUpdates() } func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) { switch(type){ case .insert: if let indexPath = newIndexPath { tableView.insertRows(at: [indexPath], with: .fade) } case .delete: if let indexPath = indexPath { tableView.deleteRows(at: [indexPath], with: .fade) } case .move: if let indexPath = indexPath, let newIndexPath = newIndexPath { tableView.moveRow(at: indexPath, to: newIndexPath) } case .update: if let indexPath = indexPath, let cell = tableView.cellForRow(at: indexPath){ cell.textLabel?.text = "Hello" } } } func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { tableView.endUpdates() } } ,但是即使附加了数据集 dataCancer ,上面的命令也不会转换数据集变量 Classification 成为一个因素。它只会在控制台上返回一个因子。

由于您希望模型适合训练数据集,因此可以指定

as.factor(dataCancer$Classification)

或在 glm 行代码中使用 as.factor 函数

training_data$Classification <- as.factor(training_data$Classification)
classification_model <- glm(Classification ~ ., data = 
                           training_data, family = binomial) 

答案 1 :(得分:1)

您需要将因变量重新编码为0,1,因此请使用以下代码。

library(car)

dataCancer$Classification <- recode(dataCancer$Classification, "1=0; 2=1")

答案 2 :(得分:1)

classification_model = glm(分类〜。,data = training_data,family =二项式) eval(family $ initialize)中的错误:y值必须为0 <= y <= 1

这是因为您的数据包含数字值,而不是因子值。希望你做到了

dataCancer $ Classification <-as.factor(dataCancer $ Classification)

理想情况下,只要是1,0或1,2就没有关系。但是,如果执行上述操作也无济于事,则可以尝试将1,2转换为1,0,然后尝试相同的代码。

第二个错误当然是因为根本没有创建逻辑回归变量。