aboutsummaryrefslogtreecommitdiff
path: root/R_LogR/main.r
blob: 748b0f463d829fec83b0c828a1cdbe87af3fea0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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)))