Import the dataset
infections1 <- read.table("PopInf.txt",header=FALSE)
Our data frame does have column headers so we need to import it with header=TRUE instead
Import the dataset again with headers
infections <- read.table("PopInf.txt", header = TRUE)
Class of the dataframe
class(infections)
## [1] "data.frame"
Show the first 6 to check everything is okay and to have an quick view of our dataframe. 6 first lines of column “pop” for population and “infections” for infections
head(infections)
Run the modle of the linear modle in order to see if the variable population affect the variable infections or not
lm1 <- lm(infections$infections ~ infections$pop)
Do summary of the linear regression model to check differnet values and especially to check the P value and see the significance of variable
summary(lm1)
##
## Call:
## lm(formula = infections$infections ~ infections$pop)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1242.9 -635.5 -537.3 -367.5 6085.0
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.275e+02 3.126e+02 2.007 0.054826 .
## infections$pop 3.601e-03 9.668e-04 3.725 0.000912 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1528 on 27 degrees of freedom
## Multiple R-squared: 0.3394, Adjusted R-squared: 0.315
## F-statistic: 13.87 on 1 and 27 DF, p-value: 0.0009122
Above, as we can see p Value is smalles than 0.05 so we reject the null hypotesis which was “Pop does not affect infections”
Attributes’ function show us all the tools that our lrm has.
attributes(lm1)
## $names
## [1] "coefficients" "residuals" "effects" "rank"
## [5] "fitted.values" "assign" "qr" "df.residual"
## [9] "xlevels" "call" "terms" "model"
##
## $class
## [1] "lm"
With the coefficients tool we can see that every 1000 more people a population have the infection increase a 3.6 Also In a population of 0 We can see that the coefficients will be 6.3
lm1$coefficients
## (Intercept) infections$pop
## 6.275345e+02 3.601114e-03
Above we used the R lenguage to analyze a simple linear regression model problem with just one variable (pop) affecting infections.
Import new dataset with a new column. We are going to try to see if these two variables together affect the infection or not.
moreinfections <- read.table("Infections1.txt", header = TRUE)
Run a new modle of the linear modle checking if Population (pop) and Undentify flighing Object (ufo), affect infections
lm2 <- lm(moreinfections$infections ~ moreinfections$pop + moreinfections$ufo2010)
Do summary of the linear regression model to check differnet values and especially to check the P value and see the significance of variable
summary(lm2)
##
## Call:
## lm(formula = moreinfections$infections ~ moreinfections$pop +
## moreinfections$ufo2010)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1210.1 -595.9 -510.3 -192.3 6100.0
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.187e+02 3.129e+02 1.977 0.0587 .
## moreinfections$pop 9.281e-04 2.878e-03 0.322 0.7497
## moreinfections$ufo2010 2.235e+01 2.267e+01 0.986 0.3332
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1528 on 26 degrees of freedom
## Multiple R-squared: 0.3632, Adjusted R-squared: 0.3143
## F-statistic: 7.416 on 2 and 26 DF, p-value: 0.002829
As we can see p Value is grather than 0.05 so we do not have enough proves to reject the null hypotesis which was “Pop and UFO does not affect infections”. Fail to reject