-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelfitting.Rmd
55 lines (41 loc) · 1.77 KB
/
modelfitting.Rmd
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
<h4>Fitting a Model with \<code>R</code></h4>
The R command <tt>lm()</tt> is used to fit linear models. Firstly the response variable $y$ is specified, then the predictor variable $x$.
<p>
The tilde sign is used to denote the dependent relationship (i.e. y depends on x). The regression coefficients are then determined.
<p>
<pre><code>
lm(Y~X) # y depends on X
</code></pre>
The output will include the formula, and two coefficient terms
<ul>
<li> The intercept estimate is recorded under $(Intercept)$
<li> The slope estimate is recorded under the name of the predictor variable (here : $X$ ).
</ul>
<pre><code>
Call:
lm(formula = Y ~ X)
Coefficients:
(Intercept) X
0.7812 0.8581
</code></pre>
A more detailed data output (i.e. more than just the coefficients) is generated in the form of a data object, using the <code>summary()</code> command.
<!-- ######################################################################################### -->
<h3>Linear regression model</h3>
We can draw a scatterplot to assess the relationship between X and Y.
The commmand we will use is plot.
<ul>
<li> To fit a linear model, the relevant r command is <code>lm()</code>.
Lets save the output as a data object called <b><i>fit1</i></b>.
<li><code>summary()</code> is a very useful command when using Linear models.
</ul>
<!-- ######################################################################################### -->
<h3>Fitting a Regression Model</h3>
A regression model is fitted using the <tt>lm()</tt> command.
<br>
Consider the response variable $F$ and predictor variable $C$.
<pre><code>
C=c(0,2,4,6,8,10,12)
F=c(2.1,5.0,9.0,12.6,17.3,21.0,24.7)
Fit1=lm(F~C)
</code></pre>
<!-- ######################################################################################### -->