-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGenerating_Data_for_Linear_Modelling_Exercise.R
75 lines (60 loc) · 2.97 KB
/
Generating_Data_for_Linear_Modelling_Exercise.R
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# This an R Code File for the Introduction to R Course available at
# https://github.com/brfitzpatrick/Intro_to_R
# Copyright (C) 2015 Ben R. Fitzpatrick.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# The course author may be contacted by email at
################################################################################
# #
# Code File to Generate the Data for Module 2 #
# #
# Linear Regression in R #
# #
################################################################################
n = 5000
x1 <- seq(from = -1, to = 1, length.out = n)
x2 <- seq(from = -1, to = 1, length.out = n)
Full.Comb <- expand.grid(x1, x2)
colnames(Full.Comb) <- c('x1', 'x2')
Sample = Full.Comb[sample(x = 1:nrow(Full.Comb), size = 2000, replace = FALSE),]
X <- data.frame(x1 = Sample$x1,
x2 = Sample$x2,
x1.2 = Sample$x1^2,
x2.2 = Sample$x2^2,
x1.3 = Sample$x1^3,
x2.3 = Sample$x2^3,
x1.4 = Sample$x1^4,
x2.4 = Sample$x2^4,
x1x2 = Sample$x1* Sample$x2)
beta <- c(-1, 1, 4, 2, -2, -1, 3)
y <- beta[1] +
beta[2]*X$x1 +
beta[3]*X$x1.2 +
beta[4]*X$x1.3 +
beta[5]*X$x2.2 +
beta[6]*X$x2.4 +
beta[7]*X$x1*X$x2
y <- y + rnorm(n = length(y), mean = 0, sd = 0.75)
Data <- data.frame(y, Sample)
setwd('~/Intro_to_R/Data/Linear_Modelling/')
write.csv(x = Data, row.names = FALSE, file = 'Multiple_Regression_Data.csv')
################################################################################
# #
# End of Code File to Generate the Data for Module 2 #
# #
# Linear Regression in R #
# #
################################################################################