data(iris)
# Convert the Species to a binary factor: 'setosa' or 'not setosa'
iris$Species <- factor(ifelse(iris$Species == 'setosa', 'setosa', 'not setosa'))
# Logistic regression model
model <- glm(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
data = iris, family = binomial())
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
# Summary of the model to get coefficients
summary(model)
##
## Call:
## glm(formula = Species ~ Sepal.Length + Sepal.Width + Petal.Length +
## Petal.Width, family = binomial(), data = iris)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -16.946 457457.097 0 1
## Sepal.Length 11.759 130504.042 0 1
## Sepal.Width 7.842 59415.385 0 1
## Petal.Length -20.088 107724.594 0 1
## Petal.Width -21.608 154350.616 0 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1.9095e+02 on 149 degrees of freedom
## Residual deviance: 3.2940e-09 on 145 degrees of freedom
## AIC: 10
##
## Number of Fisher Scoring iterations: 25
library(ggplot2)
# Create a data frame for predictions
pred_data <- data.frame(
Sepal.Length = rep(seq(min(iris$Sepal.Length), max(iris$Sepal.Length), length.out = 100), 400),
Sepal.Width = rep(seq(min(iris$Sepal.Width), max(iris$Sepal.Width), length.out = 100), each = 100),
Petal.Length = mean(iris$Petal.Length),
Petal.Width = mean(iris$Petal.Width)
)
# Predict probabilities
pred_data$Species <- predict(model, newdata = pred_data, type = "response")
# Create the plot
ggplot(pred_data, aes(x = Sepal.Length, y = Species, color = Sepal.Width)) +
geom_line() +
labs(title = "Probability of being Setosa based on Sepal Measurements",
x = "Sepal Length",
y = "Probability",
color = "Sepal Width") +
theme_minimal()
Why you should not run a multivariate regression?
Logistic regression is used when the dependent variable is binary, as in this case (‘setosa’ or ‘not setosa’). Multivariate regression, on the other hand, is suited for continuous dependent variables. Using multivariate regression on a binary outcome would not appropriately model the probability distribution of the dependent variable and could lead to incorrect inferences.
This semester, I had the opportunity to embark on an enriching journey through the world of data analysis, a course that not only broadened my understanding of key analytical concepts but also sharpened my technical skills, especially in R programming. The course was a blend of theoretical knowledge and practical application, making it both challenging and immensely rewarding.
Data Set Management in R: One of the first and most crucial skills I learned was how to manage datasets in R. This included importing, cleaning, and transforming data, which are essential steps in preparing for any analysis. Mastering these techniques allowed me to efficiently handle real-world data, setting a strong foundation for more complex analyses.
Probability Concepts: Delving into probability helped me understand the underpinnings of statistical analysis. I learned about various probability distributions and how they are applied in data analysis, which is critical for interpreting and making predictions based on data.
Establishing and Computing Distributions: Building on probability knowledge, the course taught me how to establish and compute different distributions. This skill is fundamental in statistical modeling and aids in understanding how data points are spread out or concentrated across a range.
Ordinary Least Squares (OLS): OLS was a significant part of the curriculum. Learning about this linear regression technique, its assumptions, and applications was fascinating. It gave me insights into how relationships between variables can be quantified and predicted.
Logistic Regression: Another highlight was exploring logistic regression, a powerful tool for binary and categorical outcome prediction. Understanding the logistic function and how it’s used in modeling binary outcomes was particularly intriguing and useful.
This course was not just about learning R and statistical methods; it was a transformative experience that enhanced my analytical thinking and problem-solving skills. The hands-on approach with real data sets made the learning process more dynamic and applicable to real-world scenarios. I am now more confident in my ability to analyze complex datasets and draw meaningful conclusions.
Looking forward, I am excited to apply these skills in various domains, be it academic research or professional projects. The knowledge of OLS and logistic regression, in particular, are assets that I anticipate will be valuable in my future endeavors in data analysis.