diff options
Diffstat (limited to 'R_LogR/main.r')
| -rwxr-xr-x | R_LogR/main.r | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/R_LogR/main.r b/R_LogR/main.r new file mode 100755 index 0000000..748b0f4 --- /dev/null +++ b/R_LogR/main.r @@ -0,0 +1,41 @@ +#!/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))) |
