<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://eclr.humanities.manchester.ac.uk/index.php?action=history&amp;feed=atom&amp;title=Predicting_from_Regression_in_R</id>
		<title>Predicting from Regression in R - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://eclr.humanities.manchester.ac.uk/index.php?action=history&amp;feed=atom&amp;title=Predicting_from_Regression_in_R"/>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Predicting_from_Regression_in_R&amp;action=history"/>
		<updated>2026-04-28T03:13:56Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.30.1</generator>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Predicting_from_Regression_in_R&amp;diff=3996&amp;oldid=prev</id>
		<title>Rb at 20:22, 7 August 2015</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Predicting_from_Regression_in_R&amp;diff=3996&amp;oldid=prev"/>
				<updated>2015-08-07T20:22:26Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 20:22, 7 August 2015&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot; &gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Here we will be using the Women&amp;#039;s Wages dataset which is made available [[R|here]].&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;= Introduction and Data Upload =&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;= Introduction and Data Upload =&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Rb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Predicting_from_Regression_in_R&amp;diff=3995&amp;oldid=prev</id>
		<title>Rb at 20:19, 7 August 2015</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Predicting_from_Regression_in_R&amp;diff=3995&amp;oldid=prev"/>
				<updated>2015-08-07T20:19:31Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 20:19, 7 August 2015&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l95&quot; &gt;Line 95:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 95:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;##&amp;#160; 1.39745636 -0.05580818&amp;#160; 2.85072091&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;##&amp;#160; 1.39745636 -0.05580818&amp;#160; 2.85072091&amp;lt;/pre&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We can see that the confidence interval is now much narrower, although we still have the same expected value of 1.40. The question this type of interval asks is the following. Assume you have many women aged 20 and with 8 years of education. What should you expect the average wage of these many women to be. Answer: With 99% probability, the model tells us, this value should be between -0.06 and 2.85.&lt;/div&gt;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;We can see that the confidence interval is now much narrower, although we still have the same expected value of 1.40. The question this type of interval asks is the following. Assume you have many women aged 20 and with 8 years of education. What should you expect the average wage of these many women to be. Answer: With 99% probability, the model tells us, this value should be between -0.06 and 2.85.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;We can see that the confidence interval is now much narrower, although we still have the same expected value of 1.40. The question this type of interval asks is the following. Assume you have many women aged 20 and with 8 years of education. What should you expect the average wage of these many women to be. Answer: With 99% probability, the model tells us, this value should be between -0.06 and 2.85.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class=&#039;diff-marker&#039;&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Why does predicting an average produce such a narrower prediction interval. As you are calculating averages you are averaging out individual variation and hence we don&amp;#039;t need to take that into account. This tends to massively reduce the width of the confidence interval.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Rb</name></author>	</entry>

	<entry>
		<id>http://eclr.humanities.manchester.ac.uk/index.php?title=Predicting_from_Regression_in_R&amp;diff=3994&amp;oldid=prev</id>
		<title>Rb: Created page with &quot;= Introduction and Data Upload =  Set your working directory and load data  &lt;pre class=&quot;r&quot;&gt;setwd(&amp;quot;C:/Users/Ralph Becker/Dropbox/R&amp;quot;)   # This sets the working directo...&quot;</title>
		<link rel="alternate" type="text/html" href="http://eclr.humanities.manchester.ac.uk/index.php?title=Predicting_from_Regression_in_R&amp;diff=3994&amp;oldid=prev"/>
				<updated>2015-08-07T20:15:57Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;= Introduction and Data Upload =  Set your working directory and load data  &amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;setwd(&amp;quot;C:/Users/Ralph Becker/Dropbox/R&amp;quot;)   # This sets the working directo...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Introduction and Data Upload =&lt;br /&gt;
&lt;br /&gt;
Set your working directory and load data&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;setwd(&amp;amp;quot;C:/Users/Ralph Becker/Dropbox/R&amp;amp;quot;)   # This sets the working directory, ensure data file is in here&lt;br /&gt;
mydata &amp;amp;lt;- read.csv(&amp;amp;quot;Mroz.csv&amp;amp;quot;,na.strings = &amp;amp;quot;.&amp;amp;quot;)  # Opens mroz.csv from working directory&amp;lt;/pre&amp;gt;&lt;br /&gt;
Let&amp;#039;s look at the &amp;lt;code&amp;gt;wage&amp;lt;/code&amp;gt; variable ([http://eclr.humanities.manchester.ac.uk/index.php/MROZ_Variable_Description Variable Descriptions]):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;summary(mydata$wage)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA&amp;#039;s &lt;br /&gt;
##  0.1282  2.2630  3.4820  4.1780  4.9710 25.0000     325&amp;lt;/pre&amp;gt;&lt;br /&gt;
In our dataset we have 325 observations that have no wage information. You will have to be clear what the reason for this is. Are they not working, or are these genuinly missing observations? The clue here is in the &amp;lt;code&amp;gt;inlf&amp;lt;/code&amp;gt; variable which indicates whether a particular women is in the labour force (inlf = 1) or not (inlf = 0). All those observations for which the wage is missing coincide with (inlf = 0). You can see that from the spreadsheet or by checking&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;mydata$inlf[is.na(mydata$wage)]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;##   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;br /&gt;
##  [36] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;br /&gt;
##  [71] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;br /&gt;
## [106] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;br /&gt;
## [141] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;br /&gt;
## [176] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;br /&gt;
## [211] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;br /&gt;
## [246] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;br /&gt;
## [281] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0&lt;br /&gt;
## [316] 0 0 0 0 0 0 0 0 0 0&amp;lt;/pre&amp;gt;&lt;br /&gt;
The &amp;lt;code&amp;gt;is.na( )&amp;lt;/code&amp;gt; function is a very useful one. It checks whether the elemets in &amp;lt;code&amp;gt;mydata$wage&amp;lt;/code&amp;gt; are missing or not. It returns values of either &amp;amp;quot;TRUE&amp;amp;quot; if they are missing or &amp;amp;quot;FALSE&amp;amp;quot; for observations where the wage is not missing. When you put this function into square brackets after &amp;lt;code&amp;gt;mydata$inlf&amp;lt;/code&amp;gt; then R will only return those values of the &amp;lt;code&amp;gt;inlf&amp;lt;/code&amp;gt; variable where the corresponding value for the &amp;lt;code&amp;gt;wage&amp;lt;/code&amp;gt; was missing (as this would produce a &amp;amp;quot;TRUE&amp;amp;quot; from the &amp;lt;code&amp;gt;is.na( )&amp;lt;/code&amp;gt; function.)&lt;br /&gt;
&lt;br /&gt;
What we are about to do is the following. We shall first estimate the following regression model:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;math&amp;gt;wage_i = \alpha_0 + \alpha_1 age_i + \alpha_2 educ_i + eps_i&amp;lt;/math&amp;gt;&lt;br /&gt;
&lt;br /&gt;
on the basis of all data have wage information. Then, subsequently we shall use the estimated model to predict the wages of those who are not currently working. I.e. we&amp;#039;d be answering the question of how much they should expect to earn (on the basis of their age and education).&lt;br /&gt;
&lt;br /&gt;
Before we do this, there are two reasons of why we should look at this analysis with a pinch of salt&lt;br /&gt;
&lt;br /&gt;
# Including the education variable in such an analysis is likely to be problematic as it is likely to be correlated with the error terms. This will produce biased coefficient estimates (also see the section on [http://eclr.humanities.manchester.ac.uk/index.php/IV_in_R Instrumental variables estimation])&lt;br /&gt;
# The reason why the women is most likely not a random decision and therefore they are not a randomly selected subset. Therefore it may well be that the realtionship we estimated between wage and the explanatory variables on the basis of those women who do work may not be the same for women who do not work.&lt;br /&gt;
&lt;br /&gt;
= Regression and Prediction =&lt;br /&gt;
&lt;br /&gt;
Despite these reservations we shall use this example to demonstrate how we use R to produce predictions. Let&amp;#039;s start by splitting our dataframe into the two subgroups (those for whom we do have wages and those for whom we dont&amp;#039;t). and we shall again use the &amp;lt;code&amp;gt;is.na( )&amp;lt;/code&amp;gt; function to gether with the &amp;lt;code&amp;gt;subset&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;mydata_est &amp;amp;lt;- subset(mydata, is.na(wage) == FALSE)&lt;br /&gt;
mydata_pred &amp;amp;lt;- subset(mydata, is.na(wage) == TRUE)&amp;lt;/pre&amp;gt;&lt;br /&gt;
Those observations for which wage is missing go into the &amp;lt;code&amp;gt;mydata_pred&amp;lt;/code&amp;gt; dataframe and all others into &amp;lt;code&amp;gt;mydata_est&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We now run our regresison on the data in the &amp;lt;code&amp;gt;mydata_est&amp;lt;/code&amp;gt; dataframe&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;lm1 &amp;amp;lt;- lm(wage~age+educ, data=mydata_est)&amp;lt;/pre&amp;gt;&lt;br /&gt;
All the results are in &amp;lt;code&amp;gt;lm1&amp;lt;/code&amp;gt;. we now use this estimated model to produce predicted wages for all those observations that are in &amp;lt;code&amp;gt;mydata_pred&amp;lt;/code&amp;gt;. To do this we use the &amp;lt;code&amp;gt;predict) _&amp;lt;/code&amp;gt;function. We need to hand in the estimated model &amp;lt;code&amp;gt;lm1&amp;lt;/code&amp;gt; and then the dataframe to which we want to apply the estimated model to produce forecasts (&amp;lt;code&amp;gt;mydata_pred&amp;lt;/code&amp;gt;). This, of course, requires that this dataframe has a &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;educ&amp;lt;/code&amp;gt; variable.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;pr1 &amp;amp;lt;- predict(lm1,mydata_pred)&amp;lt;/pre&amp;gt;&lt;br /&gt;
Check out the new object &amp;lt;code&amp;gt;pr1&amp;lt;/code&amp;gt;. It is a vector with 325 predictions for the 325 observations in &amp;lt;code&amp;gt;mydata_pred&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== New data ==&lt;br /&gt;
&lt;br /&gt;
Sometimes you don&amp;#039;t have actual observations for which you want to produce forecast, but rather you have a set of conditioning values at which you want to clculate predictions.&lt;br /&gt;
&lt;br /&gt;
Now we will show how you would set this up. You would basically want to produce a new dataframe which contains, at the least, two variables named &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;educ&amp;lt;/code&amp;gt;. You could do this as follows (try and understand what age/education combinations are being produced here:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;age = seq(20, 55, 1)  # creates ages 20, 21, 22, ..., 54, 55 , 36 observations&lt;br /&gt;
educ = rep(c(8,10,12,14),9) # creates 9 repetions of 8,10,12,14, altogether 36 observations&lt;br /&gt;
new_df &amp;amp;lt;- data.frame(age,educ)&amp;lt;/pre&amp;gt;&lt;br /&gt;
And now we apply the estimated model &amp;lt;code&amp;gt;lm1&amp;lt;/code&amp;gt; to this new dataset.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;pr2 &amp;amp;lt;- predict(lm1,new_df)&amp;lt;/pre&amp;gt;&lt;br /&gt;
Of course you could have asked for forecasts for very different age/education combinations. The &amp;lt;code&amp;gt;predict( )&amp;lt;/code&amp;gt; function has a few extra bells and whistles you could use. You know well (or at least you should) that when we make predictions using a linear regresison model we get the conditional expectation, but there wil be uncertainlty around this expectation. One of the options if the &amp;lt;code&amp;gt;predict&amp;lt;/code&amp;gt; function allows you to get confidence intervals.&lt;br /&gt;
&lt;br /&gt;
Let&amp;#039;s try the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;pr3 &amp;amp;lt;- predict(lm1,new_df, level = 0.99, interval = &amp;amp;quot;prediction&amp;amp;quot;)&amp;lt;/pre&amp;gt;&lt;br /&gt;
here we set the confidence &amp;lt;code&amp;gt;level&amp;lt;/code&amp;gt; to 0.99 (if you don&amp;#039;t set it explicitly R will use a default of 0.95) and ask R to produce prediction intervals. Look at the resulting &amp;lt;code&amp;gt;pr3&amp;lt;/code&amp;gt; object, it now has three columns. Let&amp;#039;s loook at the 1st row of &amp;lt;code&amp;gt;new_df&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;pr3&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;new_df[1,]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;##   age educ&lt;br /&gt;
## 1  20    8&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;pr3[1,]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;##       fit       lwr       upr &lt;br /&gt;
##  1.397456 -6.789651  9.584564&amp;lt;/pre&amp;gt;&lt;br /&gt;
The 1st of our new cases is for someone with age 20 and 8 years of education. The forecast we produced is 1.40 with lower confidence interval boundary at -3.82 and the upper boundary at 6.61. So the model tells us that there is a 99% probability that a women aged 20 with 8 years of education would earn something between -3.82 and 6.61 dollars per hour. Clearly this is a huge confidence interval and in fact it reaches into negative hourly wages which don&amp;#039;t really make sense. There are three reasons for this.&lt;br /&gt;
&lt;br /&gt;
# We set a high level of confidence, resulting in a very wide interval&lt;br /&gt;
# Our conditioning values were at the lower range of observed ages and education levels. If we had used conditioning values closer to the respective averages we would get a narrower interval (try it!)&lt;br /&gt;
# We asked R to get us an interval for an individual ()by using &amp;lt;code&amp;gt;interval = &amp;amp;quot;prediction&amp;amp;quot;&amp;lt;/code&amp;gt; which means that R also factors is the observed level of error variance.&lt;br /&gt;
&lt;br /&gt;
The last point is important as it allows us to distinguish this from a slightly different type of confidence interval:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;pr4 &amp;amp;lt;- predict(lm1,new_df, level = 0.99, interval = &amp;amp;quot;confidence&amp;amp;quot;)&amp;lt;/pre&amp;gt;&lt;br /&gt;
All that has changed is the type of interval we asked for, &amp;lt;code&amp;gt;interval = &amp;amp;quot;confidence&amp;amp;quot;&amp;lt;/code&amp;gt;. Before discussing what the difference is, let&amp;#039;s see how the result changes&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;new_df[1,]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;##   age educ&lt;br /&gt;
## 1  20    8&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre class=&amp;quot;r&amp;quot;&amp;gt;pr4[1,]&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;##         fit         lwr         upr &lt;br /&gt;
##  1.39745636 -0.05580818  2.85072091&amp;lt;/pre&amp;gt;&lt;br /&gt;
We can see that the confidence interval is now much narrower, although we still have the same expected value of 1.40. The question this type of interval asks is the following. Assume you have many women aged 20 and with 8 years of education. What should you expect the average wage of these many women to be. Answer: With 99% probability, the model tells us, this value should be between -0.06 and 2.85.&lt;/div&gt;</summary>
		<author><name>Rb</name></author>	</entry>

	</feed>