#!/usr/bin/env Rscript # https://www.r-bloggers.com/2015/09/how-to-perform-a-logistic-regression-in-r/ data <- read.csv("survey.csv") str(data) head(data) data$price20 <- ifelse(data$Price == 20, 1, 0) data$price30 <- ifelse(data$Price == 30, 1, 0) head(data) model <- glm( MYDEPV ~ Income + Age + price20 + price30, family = binomial(link = "logit"), data = data ) summary(model) coef(model) plot(data$Income, data$MYDEPV) test_dat <- data.frame(Income = seq(20, 100, 1), Age = 20, price20 = 1, price30 = 0) pred <- predict(model, newdata = test_dat, type = "response") lines(test_dat$Income, pred, col = "blue", lwd = 2) new_data3 <- data.frame( Income = c(58), Age = c(25), price20 = c(1), price30 = c(0) ) predicted <- predict(model, newdata = new_data3) print(1 / (1 + exp(-predicted)))