forked from DoubleD1994/R_Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateStatisticalModels.R
More file actions
50 lines (41 loc) · 826 Bytes
/
Copy pathCreateStatisticalModels.R
File metadata and controls
50 lines (41 loc) · 826 Bytes
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
42
43
44
45
46
47
48
49
50
# Creating Statistical Model
# Set working directory
setwd("/Users/daviddryburgh/Documents/R_Programming")
# Load CSV file
iris <- read.csv("Iris.csv")
# Peak at data
head(iris)
# Create a scatterplot
plot(
x = iris$Petal.Length,
y = iris$Petal.Width,
main = "Iris Petal Length vs. Width",
xlab = "Petal Length (cm)",
ylab = "Petal width (cm)"
)
# Create a linear regression model
model <- lm(
formula = Petal.Width ~ Petal.Length,
data = iris
)
# Summarize the model
summary(model)
# Draw a regression line on plot
lines(
x = iris$Petal.Length,
y = model$fitted,
col = "red",
lwd = 3
)
# Get correlation coefficient
cor(
x = iris$Petal.Length,
y = iris$Petal.Width
)
# Predict new values from the model
predict(
object = model,
newdata = data.frame(
Petal.Length = c(2, 5, 7)
)
)