Regression R Scripts

The R script for the linear model  y = mx and the nonlinear quadratic model y = ax^2.

The following code did the regression calculations and graphing for the Regression Example 1’s solution. You can copy and paste the code into R and it should run without modification. You can download R for free from  R  https://www.r-project.org/ or you can copy and paste the code into a free online R compiler such as: rextester   https://rextester.com/l/r_online_compiler or JDOODLE  https://www.jdoodle.com/execute-r-online

 

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# R script for regression examples with Latex
# models y = mx and y = ax^2
RegressionModel = "L";   #"L" = linear model y = mx; "P" = parabolic model y = ax^2
 
#( -3, 0), (-5,2) Data from Regression HW problem
x = c(-3,-5);
y = c(0, 2);
 
# Compare your own "guess" of "m" or "a" to the regression "m" or "a".
#---------------------------------------------------------------------
CompareModel = "N"     # "Y" or "N". If "Y", competing model graphed and its SSE calculated.
Guess_m_a = .5;      # enter numerical value for m or for a. Default is .5.
 
# DO NOT CHANGE ANYTHING BELOW HERE!!!!!!!!!!!
#----------------------------------------------
ShowDeviateBars = "Y"; # "Y" or "N"
GraphPaper = FALSE; #TRUE = prints graph paper; FALSE = plots regression line
ErrorString = "";
CompareModelsString = "Thank you for using R.";
By = 1
#------------------------------------------------------------
if(RegressionModel == "P")
{ f = function(x){x^2;};
a = sum(y*f(x))/sum(f(x)^2); a_3 = round(a,3);
yxx = y*x^2;
xxxx = x^4;
errors = y - a_3*f(x);
errors2 = round(errors^2,3);
SSE = sum( (y - a_3*f(x))^2 ); SSE3 = round(SSE,3); # SSE;
a3x2 = a_3*x^2;
df1 = data.frame(x,y, yxx, xxxx, a3x2, errors, errors2); # df1;
if(ShowDeviateBars == "Y"){ErrorString = "\nErrors shown in red. SSE = ";}
mainTitle = paste("Data points with regression curve y = ", toString(a_3), "x^2",ErrorString, toString(SSE3));
if(GraphPaper){mainTitle = "(d) Plot the data points and the regression curve y = a x^2";};
}
if(RegressionModel == "P"){
sum_yxx = sum(yxx);
sum_xxxx = sum(xxxx);
SSE_sum_errors2 = sum(errors2);
df2 = data.frame(sum_yxx, sum_xxxx, a_3, SSE_sum_errors2); #df2;
}
#------------------------------------------------------------
#------------------------------------------------------------
if(RegressionModel == "L")
{ f = function(x){x;};
a = sum(y*f(x))/sum(f(x)^2); a_3 = round(a,3);
yx = y*x;
xx = x^2;
errors = y - a_3*f(x);
errors2 = round(errors^2,3);
sum_yx = sum(yx); # sum_yx;
sum_xx = sum(xx); # sum_xx;
SSE = sum( (y - a_3*f(x))^2 ); SSE3 = round(SSE,3); # SSE;
mx = a_3*x;
df1 = data.frame(x,y, yx, xx, mx, errors, errors2); # df1;
if(ShowDeviateBars == "Y"){ErrorString = "\nErrors shown in red. SSE = ";}
mainTitle = paste("Data points with regression line y = ", toString(a_3), "x",ErrorString, toString(SSE3));
if(GraphPaper){mainTitle = "(d) Plot the data points and the regression line y = m x";};
}
if(RegressionModel == "L"){
sum_yx = sum(yx);
sum_xx = sum(xx);
SSE_sum_errors2 = sum(errors2);
df2 = data.frame(sum_yx, sum_xx, a_3, SSE_sum_errors2); # df2;
}
#------------------------------------------------------------
 
# Range Calculatins to center graph paper
#------------------------------------------------------------
RangeY = max(y, a_3*f(x),0 ) - min(y, a_3*f(x), 0); # RangeY ;
RangeX = max(x,0) - min(x,0); # RangeX ;
if(RangeX < RangeY){
Ymin = round(min(y,0, a_3*f(x)) - 1,0); # Ymin;
Ymax = round(max(y,0, a_3*f(x)) + 1,0); # Ymax;
RangeYnew = Ymax - Ymin;
Xmin = round(min(x,0) - (RangeYnew - RangeX)/2, 0); # Xmin;
Xmax = round(max(x,0) + (RangeYnew - RangeX)/2, 0); # Xmax;
}
if(RangeY < RangeX){
Xmin = min(x,0) - 1;
Xmax = max(x,0) + 1;
Ymin = round(min(y,0, a_3*f(x)) - (RangeX - RangeY)/2,0);
Ymax = round(max(y,0,a_3*f(x)) + (RangeX - RangeY)/2,0);
}
# Ymin;
# Ymax;
# Xmin;
# Xmax;
 
# Plot graph paper
#------------------------------------------------------------
plot(1, type="n", xlab="", ylab="",
xlim=c(Xmin, Xmax ), ylim=c(Ymin , Ymax ), asp=1 , main = mainTitle );
arrows(0,Ymin, 0, Ymax, length = 0.25, angle = 30,
col = "black", lty = 1, lwd = 3);
arrows(Xmin,0, Xmax, 0, length = 0.25, angle = 30,
col = "black", lty = 1, lwd = 3)
 
xSeq = seq(Xmin, Xmax, by = By);
lenxSeq = length(xSeq);
for(i in 1:lenxSeq){
lines( c(xSeq[i], xSeq[i]), c(Ymin, Ymax), col = "blue")
}
ySeq = seq(Ymin, Ymax, by = By);
lenySeq = length(ySeq);
for(i in 1:lenySeq){
lines( c(Xmin, Xmax), c(ySeq[i], ySeq[i]), col = "blue")
}
#------------------------------------------------------------
if(GraphPaper == FALSE){
if(ShowDeviateBars == "Y"){
for(i in 1:length(x)){lines( c(x[i],x[i]), c( y[i], a_3*f(x[i]) ), col = "red", lwd = 4);};
}
points( x, y, pch = 20, cex = 3); # data
xSeq = seq(Xmin, Xmax, by = By/10);
lines( xSeq, a_3*f(xSeq), lwd = 4); # regression line
}
# ------------------
if(RegressionModel == "L"){modelStrR = paste("y = ", toString(a_3),"x", sep = "");};
if(RegressionModel == "P"){modelStrR = paste("y = ", toString(a_3),"x^2", sep = "");};
CompareModelsString = paste("The regression curve ",
modelStrR,
", drawn in black, has SSE = ",
toString(SSE3),
". Thank you for using R.",
sep = "");
# ------------------
if(CompareModel == "Y"){lines(xSeq,Guess_m_a*f(xSeq), lwd = 4, col = "blue");
SSE_compare = sum( (y - Guess_m_a*f(x))^2 );
if(RegressionModel == "L"){modelStrG = paste("y = ", toString(Guess_m_a),"x", sep = "");
modelStrR = paste("y = ", toString(a_3),"x", sep = "");};
if(RegressionModel == "P"){modelStrG = paste("y = ", toString(Guess_m_a),"x^2", sep = "");
modelStrR = paste("y = ", toString(a_3),"x^2", sep = ""); };
CompareModelsString = paste("The regression curve ",
modelStrR,
", drawn in black, has SSE = ",
toString(SSE3),
". Your model ",
modelStrG,
", drawn in blue, has SSE = ",
toString(SSE_compare),
".", sep = "");
};
CompareModelsString;
# ------ display data.frames of regression calculations
df1;
df2;
#End of R script