- 29th Jul 2022
- 06:03 am

library(readr) gdp <- read_csv("pop-gdp.csv") dim(gdp) #Renaming the columns names(gdp)[2] <- "Pop" names(gdp)[3] <- "GDP" colnames(gdp) #scatter plot library(ggplot2) ggplot(gdp, aes(x = GDP, y = Year))+geom_point() ggplot(gdp, aes(x = Pop, y = Year))+geom_point() library(dplyr) R1 <- gdp %>% filter(between(Year, 1947, 1964)) R1 R2 <- gdp %>% filter(between(Year, 1972, 1990)) R2 R3 <- gdp %>% filter(between(Year, 1998, 2014)) R3 #Years 1955 and 1960 are missing in R1 # for GDP lm.fit <- lm(GDP ~ Year, data = R1) lm.fit newdata = data.frame(Year = 1955) p1 <- predict(lm.fit, newdata) newdata1 = data.frame(Year = 1960) p2 <- predict(lm.fit, newdata1) # for Pop lm.fit1 <- lm(Pop ~ Year, data = R1) lm.fit1 newdata2 = data.frame(Year = 1955) p3 <- predict(lm.fit1, newdata2) newdata3 = data.frame(Year = 1960) p4 <- predict(lm.fit1, newdata3) ################# new_row_1 <- c(1955, p1, p3) new_row_2 <- c(1960, p2, p4) R1 <- rbind(R1, new_row_1, new_row_2) %>% arrange(Year) # Scatter plot of enhanced R1 ggplot(R1, aes(x = GDP, y = Year))+geom_point() ggplot(R1, aes(x = Pop, y = Year))+geom_point()

# As we can see that these points are totally differnet from the linear trend in the data