0% found this document useful (0 votes)
11 views5 pages

R Lab Ex 6 To 8

Uploaded by

kill2king007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

R Lab Ex 6 To 8

Uploaded by

kill2king007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

6.

REGRESSION MODEL
(A) R program to illustrate Linear Regression

# Height vector
x <- c(153, 169, 140, 186, 128,136, 178, 163, 152, 133)
# Weight vector
y <- c(64, 81, 58, 91, 47, 57,75, 72, 62, 49)
# Create a linear regression model
model <- lm(y~x)
# Print regression model
print(model)
OUTPUT:
Call:
lm(formula = y ~ x)

Coefficients:
(Intercept) x
-39.7137 0.6847

# Find the weight of a person with height 182


df <- data.frame(x = 182)
res <- predict(model, df)
cat("\nPredicted value of a person with height = 182")
print(res)
OUTPUT:
1
84.9098

# Output to be present as PNG file


png(file = "linearRegGFG.png")
# Plot
plot(x, y, main = "Height vs Weight Regression model")

OUTPUT:

abline(lm(y~x))
# Save the file.
dev.off()

(B) R program to illustrate Multiple Linear Regression

# Using airquality dataset


input <- airquality[1:50,c("Ozone", "Wind", "Temp")]

# Create regression model


model <- lm(Ozone~Wind + Temp,data = input)

# Print the regression model


cat("Regression model:\n")
print(model)

OUTPUT:
Call:
lm(formula = Ozone ~ Wind + Temp, data = input)

Coefficients:
(Intercept) Wind Temp
-58.239 -0.739 1.329

# Output to be present as PNG file


png(file = "multipleRegGFG.png")

# Plot
plot(model)
OUTPUT:
# Save the file.
dev.off()

(C) R program to illustrate Logistic Regression

# Using mtcars dataset


# To create the logistic model
model <- glm(formula = vs ~ wt,family = binomial,data = mtcars)

# Creating a range of wt values


x <- seq(min(mtcars$wt),max(mtcars$wt),0.01)

# Predict using weight


y <- predict(model, list(wt = x),type = "response")

# Print model
print(model)
OUTPUT:
Call: glm(formula = vs ~ wt, family = binomial, data = mtcars)

Coefficients:
(Intercept) wt
5.715 -1.911

Degrees of Freedom: 31 Total (i.e. Null); 30 Residual


Null Deviance: 43.86
Residual Deviance: 31.37 AIC: 35.37
# Output to be present as PNG file
png(file = "LogRegGFG.png")
# Plot
plot(mtcars$wt, mtcars$vs, pch = 16,xlab = "Weight", ylab = "VS")
lines(x, y)
OUTPUT:

# Saving the file


dev.off()

You might also like