Saturday, December 16, 2017

Assignment 6: Linear Regression

Part 1

Introduction: The goal of this assignment is to understand how to use regression to determine correlations between two variables. The regression statistic is similar to the correlation statistic; however, it utilizes Ordinary Least Square (OLS) to compute a line of best fit for the given data. OLS is a process where a line on a scatterplot is computed so total amount of distances from points on a trend line is minimized, giving the scatterplot the line of best fit.

In understanding regression analysis, there is generally a dependent and a independent variable. The dependent variable is based off of the independent variable--not the other way around. So for example: in analyzing rainfall, lets say there are two variables: the amount of rainfall, and distance from from the equator. In determining which variables are the dependent and independent variable, the amount of rainfall would be based off of distance from the equator, so distance from the equator would be the independent variable and amount of rainfall would be the dependent variable (it wouldn't make sense the other way around. Could distance from the equator be determined by rainfall? Probably not).

In computing the OLS, the result produces a "line of best fit" equation, or a linear equation. The linear equation is y = a + bx, where "a" is the constant (where the line crosses the y-axis), and b is the regression coefficient (the slope of the line). The slope determines how the dependent variable is impacted by the independent variable. The relationship can be either positive (when independent variable increases, dependent variable increases) or negative (when independent variable increases, dependent variable decreases).

In utilizing regression, strength also needs to be assessed. Strength is determined by a statistic called the coefficient of determination. The coefficient of determination is determined by r^2 (r is a statistic computed during regression analysis, in which one needs to square it to obtain the coefficient of determination). The coefficient of determination is between 0-1; numbers closer to 1 meaning a stronger relationship between the two variables, numbers closer to 0 meaning a weaker one.

In computing regression statistics, there is also a statistic called the residual. The residual determines the distance between the actual point on the scatterplot, and the line of best fit. The residual computes the distance for each point, and the sum of the standard deviation for each residual is called the standard error of the estimate. In analyzing the standard error of the estimate, the smaller the number, the more accurate the prediction.

For the first problem, we were given an excel sheet that displayed percent of kids that get free lunch in certain areas, and the crime rate per 100,000 people. The research question for the given data, is does the crime rate increase as the number of kids who obtain free lunch increase.

Methods: In determining whether or not the crime rate increases as the number of kids who obtain free lunch increases, the program SPSS was utilized to run a linear regression analysis. In this case, the crime rate is the dependent variable while the number of kids that get free lunch is the independent variable (because in theory, the crime rate is based off of the "free lunch" rate).


Results: The results for the linear regression model are found in Figure 1 and Figure 2.
Figure 1: Regression Analysis Equation of the Model

Figure 2: R-Square model of the Regression Analysis

In analyzing Figure 1 figures, the equation for the regression analysis is y = 21.819 + 1.685x. 21.819 is the constant, meaning that is where the equation crosses the y-axis, and 1.685 is the regression coefficient. The equation is a positive equation, meaning when the number of kids who obtain free lunch increases, the crime rate tends to increase. Also, the equation is significant at .005, meaning that the equation is significant at the 99% level and that there is a linear relationship between the number of kids who obtain free lunch, and the crime rate.

In analyzing Figure 2, the r-squared is .173, which on the 0-1 scale isn't the strongest trend line for the given data. However, in relating the r-squared back to the equation, the equation is significant at the 99% level, so even though the relationship isn't the strongest, it is significant.

Discussion: In analyzing the regression analysis done on the two variables, there seems to be a linear relationship between the number of kids who receive free lunch, and the crime rate despite the fact the r-squared is weak. So for example; if a new area of town was identified as having 30% kids with free lunch, in plugging the number into the equation y = 21.819 + 1.685(30), the crime rate would be 72.369 per 100,000 people. Despite the fact that the relationship between the two variables is significant at the 99% level, the r-squared is only .173, meaning its a weak relationship. If the r-squared were closer to 1, there might be more confidence in the relationship between free lunch and the crime rate, however because it's so low there isn't much confidence in the relationship.


Part 2


Introduction: For this part, we were given data associated with 911 calls per census tract in Portland, Oregon. The data set included variables such as the number of 911 calls per census tract, jobs, renters, number of people with no high school degree, alcohol sales, number of people unemployed, foreign born population, median income, and number of college grads, with the goal of seeing if there is any explanation for where higher proportion of calls come from. In this case, knowing if there's a relationship between the number of calls and other variables is useful for companies who want to know what factors influence 911 calls, and where to build a new hospital. 

Methods: In order to see which variables influence 911 calls the greatest three separate regression analysis's were done. The dependent variable in each of the regression analysis's was the number of 911 calls per census tract, while the independent variable varied per analysis. For the purposes of this assignment, the independent variables were chosen at random. 

The statistical software program SPSS was utilized to run regression analysis's. Once the three separate analysis's were run, the regression with the highest significant correlation was utilized to create a residual map in ArcMap. The highest significant correlation was utilized because it shows the greatest potential in explaining the relationship between 911 calls and other contributing factors.

Results: The three independent variables used were the number of people with no high school degree, number unemployed, and alcohol sales. In Figure 3 and Figure 4, the figures are analyzing the linearity between alcohol sales and 911 calls.
Figure 3: R-Squared Model of Relationship between Alcohol Sales and 911 calls

Figure 4: Linear Equation and Significance of the Model
In analyzing Figure 3, there seems to be a weak linear relationship between alcohol sales and the number of 911 calls. The coefficient of determination is .152, which is low on the 0-1 scale. However, in analyzing Figure 4, the significance level is lower than .05, meaning that despite the weak relationship, the relationship is significant at the 95% level. Because the relationship between the two variables is significant at the 95% level, we reject the null hypothesis (that there is no significant linear relationship between alcohol sales and the number of 911 calls), and state there is a significant relationship between the two. Also, it's important to note that the relationship is positive. The b-value for this model is 3.0695E-5, which means when calls increases by one, the alcohol sales also tend to increase by the b-value.

In Figure 5 and Figure 6, the figures are analyzing the relationship between the number of unemployed people per census tract, and 911 calls.
Figure 5: R-Squared Model of Relationship between Number of Unemployed and 911 Calls
Figure 6: Linear Equation and Significance of the Model
In analyzing Figure 5, there seems to be a moderate linear relationship between the number of people unemployed and 911 calls. The coefficient of determination is .543, which at least compared to Figure 3, is stronger by approximately .4. In analyzing Figure 6, the significance value is under .05, meaning that their is a significant linear relationship between the two variables. Because the regression is significant at the 95% level, we reject the null hypothesis (that there is no significant linear relationship between number of unemployed and the number of 911 calls), and state there is a significant linear relationship between the two variables. Also, the relationship is a positive relationship. The b-value for unemployed is .507, meaning that when the number of calls goes up by one, the number of unemployed people also increases by .507.

In Figure 7 and Figure 8, these figures are analyzing the relationship between the number of people without high school diplomas and the number of 911 calls.
Figure 7: R-Squared Model of Relationship between Number of People without High School Degrees, and 911 calls

Figure 8: Linear Equation and Significance of the Model

 In analyzing Figure 7, there seems to be a moderate linear relationship between the number of people without a high school diploma and 911 calls. The coefficient of determination is .567, which is the strongest out of all of the other regression analysis's; however, on a 0-1 scale it's about midway between 0 and 1, which means it isn't the strongest relationship. In analyzing Figure 8, the significance level is less than .05, meaning that the relationship is significant at the 95% level. Because the regression is significant at the 95% level, we reject the null hypothesis (that there is no significant linear relationship between the number of people who don't have high school diplomas and 911 calls), and state there is a linear relationship between the two variables. Also, the relationship is a positive one. The b-value is .166, which means when the number of calls goes up by 1, the number of people who don't have a high school diploma increases by .166.

In comparing the three regression analysis's together, they all were significant at the 95% level and they all were positive relationships. However, the number of unemployed and the number of people without a high school diploma's coefficient of determination were noticeably higher than the alcohol sales, meaning there seems to be a stronger relationship between those two variables and the number of 911 calls. The highest coefficient of determination was the relationship between the number of people without high school diplomas and the number of 911 calls, which means companies should be most confident with number of people without high school diplomas determining the number of 911 calls (at least compared to the other two analyses).

In knowing the strongest linear relationship was between people without high school diplomas and 911 calls, a residual map was created showing the variability of each census tract. Figure 9 is a choropleth map showing the number of 911 calls per census tract, while Figure 10 is a residual map of the number of people without high school diplomas and 911 calls.
Figure 9: Choropleth Map of the Number of 911 Calls per Census Tract

Figure 10: Residual Map of Low Education Levels versus Number of 911 Calls
In analyzing Figure 9, there seems to be a concentration of a high number of 911 calls in the north-central part of the city, and one on the south-east part of the city. There also seems to be lower concentrations of 911 calls on the outer ends of the city, at least comparatively. In adding a variable to the mix to see how each census tract's point corresponds to the linear trend line, Figure 10 shows some high variability on the two census tracts on the north side of town (the two dark red ones). The dark red census tracts means that the two values are a good amount greater than what their corresponding value should be on the trend line. However, in analyzing the map as a whole, a majority of the tracts wall within the -1.5-1.5 range (or  either light blue, yellow, or light red), which means the residual values are relatively close to fitting the trend line. In relating these map values back to the regression statistics, the coefficient of determination seems to correlate with what is displayed on the map. The coefficient of determination value was .567-showing a moderate linear relationship-and what shows on the map is a majority of residuals between -1.5 and 1.5 with a couple of outliers, which seems representative of the coefficient of determination value.

Conclusion: Overall, there seemed to be the strongest linear relationship with the number of people without high school diplomas and 911 calls. The number of people unemployed and 911 class also had a moderate relationship competitive to the high school diploma one, however the linear relationship between alcohol sales and 911 calls was by far the weakest relationship, due to its low coefficient of determination. If a company were to utilize these statistics to determine factors that contribute to a high 911 call rate, the number of people without high school diplomas has the strongest linear relationship out of the three independent variables analyzed.





Monday, December 4, 2017

Assignment 5: Correlation and Spatial Autocorrelation

Part 1

Introduction: The goal of this lab is to become familiar with the statistic correlation. Correlation is the study on how two variables relate to one another. Generally, the variables already are related in some way (i.e. free throw percentage and height: the relation is through basketball), and the statistic of correlation determines whether or not the two variables are associated with one another. In calculating the correlation statistic, the values range from 1 to -1. If a correlation statistic is close to either 1 or -1, that means they have a strong positive or negative relationship. In contrast, if the correlation value is closer to zero, that means the two variables have a weak positive or negative relationship. If the correlation is zero, then that means there is no relationship between the two variables.

A way of understanding correlation from a visual perspective is to utilize a scatterplot. Scatterplots help determine whether or not there's a strong or weak correlation between the two variables using a graph. An example of a strongly correlated scatterplot is in Figure 1, where most of the data follows a linear line.

Figure 1: Example of strongly correlated scatterplot. Taken from https://www.mrholloman.net/PS/Notes/LFNotes09/image006.png

In contrast, an example of a weaker correlated scatterplot is Figure 2, where the data follows no describable pattern.
Figure 2: Weaker Correlated Scatterplot. Taken from https://static1.squarespace.com/static/

Assignment Questions:

1.) For this question, we were given data to create a scatterplot in Excel, and find the correlation statistic in the program SPSS. The two variables given to us were the distance away from the sound source measured in feet, and the sound level measure in dB.

Figure 3 depicts the scatterplot of the data, whereas Figure 4 depicts the calculated correlation statistic.


Figure 3: Scatterplot of the data

Figure 4: SPSS calculated statistic of the two variables
In analyzing Figure 3 and Figure 4, it seems like there is a strong negative correlation between the the distance away from a sound source, and the sound level. The null hypothesis states that there is no significant relationship between distance and sound level; however, we reject the null hypothesis because the correlation is significant at the 99% level (given the two stars next to the "-.896"). By rejecting the null, that means their is a relationship between distance and sound level.


2.) For this assignment, we were given data on Detroit and had to run a correlation on the data to see which two variables correlate (located in Figure 5).

The variables are:
White = White population for the 1000 census tracts in and around Detroit
Black = Black populations
Asian = Asian Pop.
His = Hispanic Pop
BachDegree = Number with a Bachelor’s Degree
MedHHInc = Median Household Income
MedHomeValue = Median Home Value
Manu = Number of Manufacturing Employees
Retail = Number of Retail Employees
Finance = Number of Finance Employees

Figure 5: Correlation statistics of Detroit

In analyzing the statistics, the highest correlation is between median household income and median home value at .883. The relationship between median household income and median home value is positive and significant at the 99% level, meaning the null hypothesis (that the two variables' relation is not statistically significant), is rejected, meaning there is a correlation between the two .

The next highest correlation is bachelor degrees and both median household income/median home value at .753. The statistic is a positive number, meaning the higher the number of bachelor degrees, the higher the median home value/median household income. The number is also statistically significant at the 99% level, meaning the null hypothesis (that the two variables' relation is not statistically significant), is rejected, meaning there is a correlation between the two.

The largest negative correlation is between white populations and black populations at -.604. The fact the relationship between those two variables is negative means the greater the white population in a given area, the smaller the black populations (or vise-versa). Also, the statistic is significant at the 99% value, meaning the null hypothesis (that the two variables' relation is not statistically significant), is rejected, meaning there is a correlation between the two.



Part 2

Introduction: The goal of this lab is to become familiar with spatial autocorrelation. Spatial autocorrelation is determining whether variables in space are correlated. For example, if the same variable is measured throughout each neighborhood, one can determine if there's a correlation between the neighborhoods and the variable through spatial autocorrelation.

In measuring spatial autocorrelation, one uses the Moran's I. The Moran's I is a similar measurement to the regular, non-spatial correlation statistic. Autocorrelation statistics closest to 1 mean there is a strong positive correlation between neighborhoods and the variable (meaning the neighborhoods are alike), while statistics closest to -1 means there is a strong negative correlation (meaning the neighborhoods are not alike). The Moran's I also utilizes four quadrants of comparison using an X, Y graph. The quadrants are divided into High, High (meaning high values are surrounded by other high values); High, Low (meaning high values are surrounded by low values); Low, High (meaning low values are surrounded by high values), and Low, Low (meaning low values are surrounded by low values). The values can be placed on a map or a graph called a LISA map to determine where spatial autocorrelations are high, low, or outliers in relation to a given area.

For this assignment, we were given data from the Texas Election Commission for the 1980 and 2012 presidential elections. The data we were given to analyze relates to Democratic votes per county in Texas, to see if there were any changes in voting patterns from 1980 to 2012. We were also given Hispanic population data from 2010 to see if population variables had any impact on voting patterns.

Methods: In order to see if there is any clustering of voting patterns throughout the state, use of the programs Excel, SPSS, and GeoDa were utilized for the creation of various statistics, graphs, and maps. Excel was useful in the creation of scatterplots for comparing correlations between population variables and voter turnout/percent voted democrat. SPSS was useful in the creation of correlation statistics for similar reasons Excel was utilized. GeoDa was useful in creating Moran's I graphs and corresponding maps for seeing if there was any clustering of the statistics.

GeoDa was the main program utilized in determining the spatial autocorrelation of the variables given to us (variables given to us were voter turnout percent for 1980 and 2012, percent democrat vote for 1980 and 2012, and percent hispanic populaion). Once all of the data was download into a shapefile, a new projected was created in GeoDa. Before calculating spatial autocorrelation statistics however, a weights manager needed to be employed so there was a spatial weight to the data. The contiguity utilized was a rook contiguity (meaning the program is only going to relate counties bordering each other), and then the statistics were all set to calculate.

After all of the Moran's I Quadrants and the Spatial Autocorrelation Maps were created, SPSS was utilized to see if there was any correlation between the variables. SPSS was utilized to see if there were any correlation between Hispanic populations and the other variables.

Results: In producing the results, a multitude of spatial autocorrelation maps and Moran's I quadrants were created.

Regarding the percent of Democratic votes in 1980, Figure 6 and Figure 7 represent the Moran's I quadrant and the spatial auto correlation map.

Figure 6: Moran's I Quadrant of the Percent Democratic Vote, 1980 

Figure 7: Spatial Autocorrelation Map of the Percent Democratic Vote, 1980

In analyzing Figure 6 and Figure 7, it seems there is a high cluster of percentage democratic votes in southern Texas, while simultaneously a high cluster of  "low percentage democrat" votes in the northern part of Texas. In analyzing the Moran's I, the statistic calculated on top of the quadrant (while not the most legible) is .57, meaning there is a moderate spatial autocorrelation between the counties and the percent of democrat vote.


Regarding the percent of Democratic votes in 2012, Figure 8 and Figure 9 represent the Moran's I quadrant and the spatial auto correlation map.

Figure 8: Moran's I Quadrant of the Percent Democratic Vote, 2012

Figure 9: Spatial Autocorrelation Map of the Percent Democratic Vote, 2012

In analyzing Figure 8 and Figure 9, there was some spatial change in the counties that voted democrat. Southern Texas still held a high-high spatial autocorrelation between the counties, meaning there was a high proportion of democrat votes in the red counties that relate to each other; however, there also seemed to be more high-high spatial autocorrelation in the western part of the state in comparison to Figure 7. In contrast, a low-low concentration of democrat votes held reign in the northern part of the state; however there seemed to be more clusters of low-low concentrations in the center part of the state compared to Figure 7. The Moran's I statistic is also higher in 2012 compared to 1980. The 2012 statistic is .69, compared to .57 in 1980, so in 2012 it seems to be more spatially correlated, however it is still only moderately correlated.

Regarding the percent of voter turnout in 1980, Figure 10 and Figure 11 represent the Moran's I and spatial autocorrelation map.
Figure 10: Moran's I Quadrant of the Percent Voter Turnout, 1980

Figure 11: Spatial Autocorrelation Map of the Percent of Voter Turnout, 1980

In analyzing Figure 10 and Figure 11, there seems to be a cluster of low-low counties in southern and eastern Texas, meaning counties with low voter turnout are surrounded by other counties of low voter turnout. In contrast, there seems to be a cluster of high-high counties in central and northern Texas, meaning counties with high voter turnout are surrounded by other counties with high voter turnout. However, the Moran's I statistic is .33, meaning that there is a weak spatial autocorrelation between counties and voter turnout, at least in 1980.

Regarding the percent of voter turnout in 2012, Figure 12 and Figure 13 represent the Moran's I and spatial autocorrelation map.
Figure 12: Moran's I Quadrant of the Percent Voter Turnout, 2012

Figure 13: Spatial Autocorrelation Map of the Percent of Voter Turnout, 2012

In analyzing Figure 12 and Figure 13, there still seems to be a cluster of low-low counties in southern Texas, and a cluster of high-high counties in comparison to Figure 11. However, the cluster of high-high counties in Northern Texas that presented itself in Figure 11 seems to have broken up in Figure 13--or at least only presented itself in the northeast corner of the state. In analyzing Figure 12, it seems the Moran's I statistic decreased compared to 1980. In 1980, the statistic was .46-a moderate spatial correlation--however in 2012 the statistic was .33, a weaker spatial autocorrelation, meaning there is less correlation of voter turnout between counties in 2012.

Regarding percent Hispanic population in 2010, Figure 14 and Figure 15 represent the Moran's I and spatial autocorrelation map.
Figure 14: Moran's I Quadrant of Percent Hispanic Population, 2010

Figure 15: Spatial Autocorrelation Map of Percent Hispanic Population, 2010

In analyzing Figure 14 and Figure 15, there seems to be a significant cluster of high-high counties stretching from southwestern Texas to southern Texas, meaning counties of high proportions of Hispanic people are surrounded by other counties with high proportions of Hispanic people. In contrast, there seems to be a significant low-low cluster in eastern Texas, meaning counties with low proportions of Hispanic people are surrounded by other counties with low proportions of Hispanic people. In analyzing Figure 14, the Moran's I statistic is .77, which means there is a moderately high spatial autocorrelation between counties and the percentage of Hispanic populations.

In bringing all of the variables together to see if there was any correlation between the five variables, Figure 16 represents a correlation matrix of the variables to see if population variables had any correlation with voting patterns.


Figure 16: Correlation Matrix of the Variables
In analyzing the statistics, there seems to be a high positive correlation between the percent of Hispanic people per county, and the percent Democrat vote in 2012, which means the higher proportion of Hispanic people per county, the more likely that county will vote Democrat. In contrast, there seems to be a high negative correlation between percent of Hispanic people per county and Voter Turnout in 2012, meaning the higher proportion of Hispanic people per county, the less likely there will be a high voter turnout. Both correlations are also significant at the 99% level, meaning the null hypothesis is rejected (the null hypothesis meaning that the two variables's relation is not statistically significant), and there is a correlation between percent Hispanic/Percent Democrat vote in 2012, and Percent Hispanic/Percent Voter Turnout in 2012.

In comparing the changes in correlation between 1980 and 2012 between the Percent Hispanic and the Percent Democrat Vote, the correlation between the two spiked dramatically. The correlation in 1980 was .09, a weak correlation between the two variables; however, in 2012 the correlation became .718, a high positive correlation between Percent Hispanic and Percent Democrat vote.

In comparing the changes between 1980 and 2012 between Percent Hispanic and the Percent Voter Turnout, the correlations between the two increased negatively. The correlation in 1980 was -.407, meaning there is a low correlation between the two variables; however, in 2012 the correlation became -.718, which is a high negative correlation between Percent Hispanic and Percent Voter Turnout.

Conclusions: In the utilization of spatial autocorrelation and correlation statistics, it was found that counties with a high percentage of Hispanic populations also tended to vote a high percentage for Democrat, however exhibited a lower voter turnout. The data was found to be significant at the 99% level, and the clusters were generally found to be in the southern portion of the state, meaning the southern counties exhibited these voting behaviors.

Sources: http://factfinder2.census.gov/faces/nav/jsf/pages/index.xhtml










Assignment 6: Linear Regression

Part 1 Introduction : The goal of this assignment is to understand how to use regression to determine correlations between two variables....