Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#################################################### # 5 . 3 Assessing Fit of Line # # Run in Groups and discuss # #################################################### install.packages ( ggplot 2 )

####################################################
# 5.3 Assessing Fit of Line #
# Run in Groups and discuss #
####################################################
install.packages("ggplot2")
install.packages("dplyr")
install.packages("broom")
install.packages("ggpubr")
library(ggplot2)
library(dplyr)
library(broom)
library(ggpubr)
#----------------------------------------------------------------------
##STEP 1- IMPORT THE DATA SET
#----------------------------------------------------------------------
#1) Download the file from blackboard and then, in RStudio, go to File > Import dataset > From Text (base).
#2) Choose the data file you have downloaded (income.data), and an Import Dataset window pops up.
#3) In the Data Frame window, you should see an X (index) column and columns listing the data for each of the variables (income and happiness).
#4) Click on the Import button and the file should appear in your Environment tab on the upper right side of the RStudio screen.
#5) After youve loaded the data, check that it has been read in correctly using summary().
summary(income.data)
#Because both our variables are quantitative, when we run this function we see a table in our console with a numeric summary of the data. This tells us the minimum, median, mean, and maximum values of the independent variable (income) and dependent variable (happiness):
#----------------------------------------------------------------------
#STEP 2- CHECK FOR NORMALITY OF THE DEPENDENT VARIABLE
#----------------------------------------------------------------------
hist(income.data$happiness)
##QUESTION 1(answer on BB): What is the shape of this distribution? Can we proceed with the linear regression? Yes or No.
#STEP 3- LINEARITY - CHECK THE RELATIONSHIP BETWEEN IND AND DEP VARIABLES
plot(happiness ~ income, data = income.data)
##QUESTION 2(answer on BB): What is the shape of this distribution? Can we proceed with the linear regression? Yes or No.
#----------------------------------------------------------------------
#STEP 4- Is there an linear relationship between income and happiness?
#----------------------------------------------------------------------
#The first line of code makes the linear model
income.happiness.lm <- lm(happiness ~ income, data = income.data)
#Notice nothing really happens. When we do the next line of code, we will see an output.
summary(income.happiness.lm)
#There's a lot of stuff here.
#QUESTION 3(answer on BB): There's a lot of stuff here. What is the value of the y-intercept (hint it's the Estimate Std. intercept)? What is the t-value (t-statistic)? What is the p-value? What is the x coefficient? What is the equation of the line in slope intercept form?
#From these results, we can say that there is a significant positive relationship between income and happiness (p value <0.001), with a 0.713-unit (+/-0.01) increase in happiness for every unit increase in income.
#----------------------------------------------------------------------
#STEP 5- GRAPH THE EQUATION OF THE LINE
#----------------------------------------------------------------------
#Next, we can plot the data and the regression line from our linear regression model so that the results can be shared.
#we're going to graph the line, the points, and then include the linear regression line on the graph along with the equation of the line and titles to make it ready for publication.
ggplot(income.data, aes(x=income, y=happiness))+ geom_point()+ geom_smooth(method ="lm", col="red")+ stat_regline_equation(label.x =3, label.y=7)+ theme_bw()+ labs(title = "Reported happiness as a function of income", x = "Income * $10,000", y = "Happiness score (1 to 10)")
,income,happiness
1,3.862647418,2.314488983
2,4.979381382,3.433489759
3,4.923956936,4.599373404
4,3.214372439,2.791113803
5,7.196409251,5.596398273
6,3.729643479,2.458555872
7,4.674517389,3.192991809
8,4.498103821,1.907136833
9,3.121630526,2.942449872
10,4.639914435,3.737941605
11,4.632839514,3.175406147
12,2.773178895,2.009046461
13,7.119478595,5.951814099
14,7.466653196,5.960547308
15,2.117742331,1.445798858
16,2.55916582,2.898583142
17,2.35479322,1.231167524
18,2.388157248,2.312988055
19,4.755680274,2.666116035
20,1.994275047,2.584729022
21,7.310916026,5.747444104
22,3.528318956,2.546524587
23,2.428751675,1.200785525
24,3.542747875,3.078293381
25,5.227201239,

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

How is cost of goods sold determined?

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago