This is an example of plotting data and fitting a line related to Keynes consumption functon mentioned in the first Chapter of Econometric Analysis by William Greene 8th Edition. I am using ggplot for plotting data. First, I am uploading data directly from the website using table and csv format. Then using library ggplot2 I shall make a plot. For very basic videos of installing R and packages and libraries, please watch videos …..
USC2000<-read.table("http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF1-1.txt",header=T,sep=",")
# USC2000<-read.csv("http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF1-1.csv") ## One may use csv file directly or by saving data in directory and uploading it from there.
Cons2000<-dput(USC2000) #dput is used to keep offline data availability
## structure(list(Year = 2000:2009, X = c(8559.4, 8883.3, 9060.1,
## 9378.1, 9937.2, 10485.9, 11268.1, 11894.1, 12238.8, 12030.3),
## C = c(6830.4, 7148.8, 7439.2, 7804, 8285.1, 8819, 9322.7,
## 9826.4, 10129.9, 10088.5)), class = "data.frame", row.names = c(NA,
## -10L))
Cons2000
## Year X C
## 1 2000 8559.4 6830.4
## 2 2001 8883.3 7148.8
## 3 2002 9060.1 7439.2
## 4 2003 9378.1 7804.0
## 5 2004 9937.2 8285.1
## 6 2005 10485.9 8819.0
## 7 2006 11268.1 9322.7
## 8 2007 11894.1 9826.4
## 9 2008 12238.8 10129.9
## 10 2009 12030.3 10088.5
You can also embed plots, for example:
## Warning: package 'ggplot2' was built under R version 3.6.3
In this part, first of all I am adding a line and then will add year alongwith dotted points to give a better picture of Keynes Consumption Function over time.
ggplot(Cons2000)+aes(X,C,label=Year)+geom_point()+geom_smooth(method = "lm",se=FALSE)+geom_text(aes(label=Year))
## `geom_smooth()` using formula 'y ~ x'
## Add Year labels
ggplot(Cons2000)+aes(X,C,label=Year)+geom_point()+geom_smooth(method = "lm",se=FALSE)+geom_text(aes(label=Year))+ labs(x="Personal Income",y="Personal Consumption")+ggtitle("Aggregate U.S. Consumption and Income Data, 2000-2009")
## `geom_smooth()` using formula 'y ~ x'
USC40 <- read.table("http://people.stern.nyu.edu/wgreene/Text/Edition7/TableF2-1.txt", header = TRUE)
df11<-dput(USC40)
## structure(list(Year = 1940:1950, X = c(241L, 280L, 319L, 331L,
## 345L, 340L, 332L, 320L, 339L, 338L, 371L), C = c(226L, 240L,
## 235L, 245L, 255L, 265L, 295L, 300L, 305L, 315L, 325L), W = c(0L,
## 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA,
## -11L))
df11
## Year X C W
## 1 1940 241 226 0
## 2 1941 280 240 0
## 3 1942 319 235 1
## 4 1943 331 245 1
## 5 1944 345 255 1
## 6 1945 340 265 1
## 7 1946 332 295 0
## 8 1947 320 300 0
## 9 1948 339 305 0
## 10 1949 338 315 0
## 11 1950 371 325 0
We regress consumption on income for whole data and separately for War-period and without War-period
ggplot(df11)+aes(x=X,y=C,color=factor(W))+geom_point()+geom_smooth(method = "lm",se=FALSE)
## `geom_smooth()` using formula 'y ~ x'
fit1<-lm(C~X,data = df11)
ggplot(df11)+aes(x=X,y=C,color=factor(W))+geom_point()+geom_smooth(method = "lm",se=FALSE)+
geom_line(mapping = aes(x=X, fitted(fit1)),color="black")
## `geom_smooth()` using formula 'y ~ x'