–Welcome to Lecture #8,Chapter V.

Let us start by finding our working directoty.

getwd()
## [1] "/cloud/project"

#installing packages that are neccessary #if installed than good, if not than install

# make sure the packages for this chapter
# are installed, install if necessary
pkg <- c("ggplot2", "scales", "maptools",
              "sp", "maps", "grid", "car" )
new.pkg <- pkg[!(pkg %in% installed.packages())]
if (length(new.pkg)) {
  install.packages(new.pkg)  
}
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
## Warning: package 'maptools' is not available for this version of R
## 
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages

Let us now make sure that the file zeroaccess is uploaded into your new project. Then let us define the variable za.

#reading csv file

# read the CSV with headers
za <- read.csv("zeroaccess.csv", header=T,sep ="," )

#above output states that if true make it the first column What is the output we have just obtained after running the chunk above.

#The data set pops up #must comment out in order too properly knit / preview file

#View(za)

#visual graph #Large sums of data put into one graph #Takes quite a while #This is a gradient graph as well, the darker the land, the more points in that geographic location #The lighter, the fewer points / reports

# Load ggplot2 to create graphics
library(ggplot2)


# create a ggplot instance with zeroaccess data
gg <- ggplot(data=za, aes(x=long, y=lat)) 
# add the points, set transparency to 1/40th 
gg <- gg + geom_point(size=1, color="#000099", alpha=1/40) 
# add axes labels
gg <- gg + xlab("Longitude") + ylab("Latitude")
# simplify the theme for aesthetics
gg <- gg + theme_bw() 
# this may take a while, over 800,000 points plotted
print(gg)

#installing new packages

install.packages("mapproj")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)

#adding them to the library

library(mapproj)
## Loading required package: maps

#It is worth noting that we are removing Antartica, because simply nobody lives there #We are making an additional visual #because we downloaded the last package, it will also appear on a world map #The points are placed using the dataset, which is made up of both lat & longitutde lines

# requires package : ggplot2
# requires object: za (5-1)
# the "maps" and "mapproj" packages are used by ggplot
# load map data of the world
world <- map_data("world")
#Remove Antarctica
world <- subset(world, world$region!="Antarctica")
# load world data into ggplot object
gg <- ggplot(data=world, aes(x=long, y=lat))
# trace along the lat/long coords by group (countries)
gg <- gg + geom_path(aes(group=group), colour="gray70")
# now project using the mercator projection
# try different projections with ?mapproject
gg <- gg + coord_map("mercator", xlim=c(-200, 200))
# load up the ZeroAccess points, overiding the default data set
gg <- gg + geom_point(data=za, aes(long, lat), 
                      colour="#000099", alpha=1/40, size=1)
# remove text, axes ticks, grid lines and do gray border on white
gg <- gg + theme(text=element_blank(), 
                 axis.ticks=element_blank(),
                 panel.grid=element_blank(),
                 panel.background=element_rect(color="gray50",
                                               fill="white"))
print(gg)

Interpret the output above.

#Let us proceed to define the file county.dataset found on Canvas.

#new Dataset

# read up census data per county
county.data <- read.csv("countydataset.csv", header=T,sep = ",")

#Commented out so it doesn’t pop up for preview/knitting purposes #if you leave in, it’ll create a pop up of the data specifically

#View(county.data)

#Random set 1

set.seed(1)
# generate 200 random numbers around 10
input <- rnorm(200, mean=10)
summary(input)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   7.785   9.386   9.951  10.036  10.613  12.402

#Minimum is 7.85 #Maximum is 12.402 #The average being 9.951 #With the most common being 10.036

Interpret the synatx and the output above.

#Creating a line graph to show the best line fit #it is important to see how many of the points do not land on the line itself #instead they are scattered, this is because it uses the average of all the data points to create the line

# requires objects: input (5-16)
# generate output around a mean of 2 x input
output <- rnorm(200, mean=input*2)
# put into data frame to plot it
our.data <- data.frame(input, output)
gg <- ggplot(our.data, aes(input, output))
gg <- gg + geom_point()
gg <- gg + geom_smooth(method = "lm", se=F, color="red")
gg <- gg + theme_bw()
print(gg)
## `geom_smooth()` using formula = 'y ~ x'

#This graph is pretty straight forward #Assumptions can be amde about this graph by just looking at it #Roughly speaking (just using the visual reference) for every 2 ouput it increases by 1 input

Make comments about both the syntax and the output of the task executed above. Is it possible to customize this graph to make it more explanatory? If so explain and run the code.

Let us now run output vs input.

model <- lm(output ~ input)
summary(model)
## 
## Call:
## lm(formula = output ~ input)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.93275 -0.54273 -0.02523  0.66833  2.58615 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.27224    0.77896   0.349    0.727    
## input        1.97692    0.07729  25.577   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.013 on 198 degrees of freedom
## Multiple R-squared:  0.7677, Adjusted R-squared:  0.7665 
## F-statistic: 654.2 on 1 and 198 DF,  p-value: < 2.2e-16

#It would be significant, because it is to the - 16th power making it certaintly less than 0.05 #It is also significant at a 1% level as well because it is a very small number Is the input significant at a 5% significance level?1%?

Let us now proceed to get the confidence interval for this model. #confint model #

confint(model)
##                 2.5 %   97.5 %
## (Intercept) -1.263895 1.808368
## input        1.824502 2.129343

Using the county.data dataset, let us run a model of Infections vs ufo2010(Aliens Visits according to the UFO). #Correlation calculations for computer infections compared to UFO sightings in 2010

summary(lm(county.data$Infections ~ county.data$ufo2010, data= county.data))
## 
## Call:
## lm(formula = county.data$Infections ~ county.data$ufo2010, data = county.data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -407.50  -70.35   -5.78   60.05 2736.39 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)         124.77740    2.38569  52.302  < 2e-16 ***
## county.data$ufo2010   0.56899    0.07998   7.114  1.4e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 127.5 on 3070 degrees of freedom
## Multiple R-squared:  0.01622,    Adjusted R-squared:  0.0159 
## F-statistic: 50.61 on 1 and 3070 DF,  p-value: 1.398e-12

#this is significant at a 5% level and at a 1% level as well #we know this because of the p value being to teh -12 exponent,

Given the output printed above, is ufo2010 significant at a 5% significance level?1%?

Let us now run a model of Infections vs every single quantitative variable that is included in the dataset.

#View(county.data)
summary(lm(Infections ~ pop + income + ipaddr + ufo2010, 
           data=county.data))
## 
## Call:
## lm(formula = Infections ~ pop + income + ipaddr + ufo2010, data = county.data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -342.94  -70.34   -5.40   58.85 2745.36 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.000e+02  9.426e+00  10.610  < 2e-16 ***
## pop         -1.615e-05  1.543e-05  -1.047  0.29510    
## income       5.670e-04  2.066e-04   2.745  0.00609 ** 
## ipaddr      -9.865e-08  5.148e-07  -0.192  0.84803    
## ufo2010      6.804e-01  1.691e-01   4.024 5.85e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 127.3 on 3067 degrees of freedom
## Multiple R-squared:  0.01879,    Adjusted R-squared:  0.01751 
## F-statistic: 14.69 on 4 and 3067 DF,  p-value: 6.915e-12

#Population & IP Address are not significant #I am suprised to see that population is not significant at first. Than I thought of how the more people in the population the lower the infections % becomes #the R Squared and Adjusted are very low %’s. It would be classified as bad in this instance based on the 0.5-1 we came up with in class #IP Address not fully needed

Interpret the output. How would you proceed from now on in this handout given the results obtained above.

#new package install.

install.packages("carData")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)

#new library we are also going to use the VIF

library(car) # for the vif() function
## Loading required package: carData

Let us just explore the variance inflation factor(VIF) of the model to see if there is a chance of high correlation between my predictors. I remind you that a strong correlation between two of my predictors will likely end up in heteroskedasticity, and therefore our model would not be accuarate.

model <- lm(Infections ~ pop + income + ipaddr + ufo2010, 
            data=county.data)
sqrt(vif(model))
##      pop   income   ipaddr  ufo2010 
## 2.165458 1.038467 1.046051 2.115512

Let us see if population affects the number of infections in this dataset. Write the null and alternative hypothesis you would use to test this relationship.

#Hypothesis: I think it will the more included in the population will decrease the % of correlation with infections #I think this because if we would at 100 million people, versus 100,000 people the numbers may get diluted

summary(lm(Infections ~ pop, data=county.data))
## 
## Call:
## lm(formula = Infections ~ pop, data = county.data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -365.26  -70.35   -5.70   59.59 2724.00 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 1.250e+02  2.416e+00  51.755  < 2e-16 ***
## pop         4.228e-05  7.147e-06   5.916 3.67e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 127.8 on 3070 degrees of freedom
## Multiple R-squared:  0.01127,    Adjusted R-squared:  0.01095 
## F-statistic:    35 on 1 and 3070 DF,  p-value: 3.668e-09

#the P value between population and infections was significant because it was lower than 5% #the r squared are extremely low #So there is in fact a correlation between the number of sightings and the amount of people in the population

Interpret the results obtained above.

Now let us define the regression of Infections vs pop as pop.lm and predict the number of infections based on the variable population.

pop.lm <- lm(Infections ~ pop, data=county.data)
predict(pop.lm, data.frame(pop=6000000), interval="confidence")
##        fit      lwr      upr
## 1 378.7109 295.9209 461.5009

#378 Infections #between 295 - 161 could be reported

Interpret the results obtained above.

Predict the number of infections based on the following values for the variable population: 1000000,2000000.

Recreate this solution building a model that depends on income.

#1 Million population data for infection rate #Obviously, lower amount of infections, as it isn’t a per/person rate.

pop.lm <- lm(Infections ~ pop, data=county.data)
predict(pop.lm, data.frame(pop=1000000), interval="confidence")
##        fit      lwr      upr
## 1 167.3069 153.9224 180.6915

#2 Million population

pop.lm <- lm(Infections ~ pop, data=county.data)
predict(pop.lm, data.frame(pop=2000000), interval="confidence")
##        fit      lwr      upr
## 1 209.5877 182.5947 236.5807

Predict the number of infections based on the following values for the variable income: 10000,20000,40000,90000.

#income of $10000

income.lm <- lm(Infections ~ income, data=county.data)
predict(income.lm, data.frame(income=10000), interval="confidence")
##        fit      lwr      upr
## 1 100.2013 85.70806 114.6945

#income of $20000

income.lm <- lm(Infections ~ income, data=county.data)
predict(income.lm, data.frame(income=20000), interval="confidence")
##        fit      lwr      upr
## 1 108.4966 97.66147 119.3318

#income of $40000

income.lm <- lm(Infections ~ income, data=county.data)
predict(income.lm, data.frame(income=40000), interval="confidence")
##        fit      lwr      upr
## 1 125.0872 120.1358 130.0387

#income of $90000

income.lm <- lm(Infections ~ income, data=county.data)
predict(income.lm, data.frame(income=90000), interval="confidence")
##        fit      lwr      upr
## 1 166.5638 148.3582 184.7694

#when looking at the income levels, I would say it is higher percentage, because they have more access to technology than someone with not as much money #this is why computer infection rates are increased as the total income goes up, simply put.