predict with linear regression in r

To make a prediction with linear regression in R, we first need to train a linear regression model using a set of training data. Once the model is trained, we can use it to make predictions on new data.

Here's an example of how to make a prediction using linear regression in R:

main.r
# Load the dataset
data <- read.csv("mydata.csv")

# Split the dataset into training and testing sets
train <- data[1:100, ]
test <- data[101:150, ]

# Train the linear regression model using the training set
model <- lm(y ~ x1 + x2 + x3, data = train)

# Make a prediction on the test set
predictions <- predict(model, newdata = test)

# Print the predicted values
print(predictions)
385 chars
16 lines

In this example, we first load a dataset and split it into training and testing sets. We then train a linear regression model using the training set, specifying the independent variables x1, x2, and x3 and the dependent variable y. Finally, we use the predict function to make a prediction on the test set and print the predicted values.

gistlibby LogSnag