In R, you can perform regression analysis, including linear regression and logistic regression. Here are some examples:
## Linear regression
# Generate some data
x <- 1:10
y <- 2*x + rnorm(10)
# Fit a linear regression model
model <- lm(y
x) summary(model) # View the model summary # Make predictions for new data new_x <- 11:15 new_y <- predict(model, newdata = data.frame(x = new_x)) # Plot the data and the regression line plot(x, y) abline(model, col = "red") In this code, we first generate some data with a linear relationship between x and y, but with some random noise using the `rnorm()` function. We then fit a linear regression model using the `lm()` function, specifying the dependent and independent variables using a formula. We can view a summary of the model using the `summary()` function. We then make predictions for new data using the `predict()` function, passing in a new data frame with the predictor variable. Finally, we plot the data and the regression line using the `plot()` and `abline()` functions. ## Logistic regression # Generate some data x <- rnorm(100) y <- rbinom(100, 1, plogis(0.5 + 1.5*x)) # Fit a logistic regression model model <- glm(y
x, family = binomial(link = "logit"))
summary(model) # View the model summary
# Make predictions for new data
new_x <- seq(-2, 2, by = 0.1)
new_y <- predict(model, newdata = data.frame(x = new_x), type = "response")
# Plot the data and the logistic curve
plot(x, y)
lines(new_x, new_y, col = "red")
In this code, we first generate some data with a logistic relationship between x and y using the `rbinom()` function with a probability function defined by the `plogis()` function. We then fit a logistic regression model using the `glm()` function, specifying the dependent and independent variables using a formula and the family and link functions for binomial logistic regression. We can view a summary of the model using the `summary()` function. We then make predictions for new data using the `predict()` function, passing in a new data frame with the predictor variable and specifying the type of prediction as "response". Finally, we plot the data and the logistic curve using the `plot()` and `lines()` functions.
Note that there are many other types of regression models available in R, including multiple linear regression, polynomial regression, and generalized linear models. You can find more information on these functions in the R documentation or online tutorials and resources.