4. ANÁLISIS DEL DÉFICIT PÚBLICO ESPAÑOL A LO LARGO DEL TIEMPO.
4.3. La España europea de la crisis (2008-actualidad).
When you have two vectors that are paired, the question arises of what their joint density looks like. Recall that when we are dealing with the density of a single random variable, the area enclosed by the density curve and theX-axis is equal to1. When we have two paired vectors, the density is a surface, and the volume between the density surface and the plane spanned by theXandY axes is now equal to1. The upper left panel of Fig- ure 4.14 illustrates what the density of a random sample of1000bivariate standard normal variates might look like. In what follows, we go through the steps required to make this density plot. Along the way, some new functions and concepts will be introduced.
First of all, we need a function for bivariate normal random numbers. The function
rnorm()is not useful here. We could use it to generate two vectors of random numbers, but these vectors will be uncorrelated. A function for generating two or more correlated vectors (brought together in a matrix), we need to load thoMASSpackage, so that the functionmvrnorm()becomes available to us. We usemvrnorm()to generate a random sample ofn= 1000paired random numbers sampled from populations with means of0, variances of1, and a correlation of0.8.
> library(MASS) > x = mvrnorm(n = 1000, mu = c(0, 0), + Sigma = cbind(c(1, 0.8), c(0.8, 1))) 105
DRAFT
X Y densitybivariate standard normal
log X log Y
density
bivariate lognormal−Poisson
log types log rank
density
lexical neighbors and rank
log Fsg log Fpl
density
singular and plural frequency
Figure 4.14: Random samples of a bivariate standard normal and a lognormal-Poisson variate (upper panels). The lower left panel shows the joint distribution of phonologi- cal neighborhood size and rank in the neighborhood for4-phoneme Dutch wordforms, the lower right panel shows the joint distribution for singular and plural frequency for monomorphemic Dutch nouns.
DRAFT
> head(x) [,1] [,2] [1,] 0.5694554 0.7122192 [2,] -1.8851621 -2.2727134 [3,] -1.7352253 -1.7685805 [4,] -1.2654685 -0.1380204 [5,] -0.2449445 -0.7448824 [6,] -1.1241598 -1.0330096We usecor()to check that the correlation between the two column vectors is indeed close to the population parameter (0.8) that we specified in the call tomvrnorm().
> cor(x[,1], x[,2]) [1] 0.7940896
The third argument ofmvrnorm(),Sigma,
> Sigma [,1] [,2] [1,] 1.0 0.8 [2,] 0.8 1.0
created withcbind(), which binds vectors column-wise, is theVARIANCE-COVARIANCE matrix of our bivariate standard normal samplex. TheSigmamatrix has the variances on the main diagonal, and theCOVARIANCESon the subdiagonal. The covariance is a measure that is closely related to the correlation. But whereas the correlation is scaled so that its values are between−1and+1, the value of the covariance can range between−∞
and+∞, and depends on the scales of its input vectors. We can illustrate the difference between the covariance and the correlation by means of the output ofmvrnorm(), which as we saw previously is a two-column matrix. The correlation of the two column vectors is the same, irrespective of whether we scale any of the vectors up, or down:
> cor(x[, 1], x[, 2]) [1] 0.7940896 > cor(x[, 1], 100 * x[, 2]) [1] 0.7940896 > cor(0.001 * x[, 1], 100 * x[, 2]) [1] 0.7940896
In contrast, the covariance changes substantially by these changes in scale:
> cov(x[, 1], x[, 2]) [1] 0.7940896 > cov(x[, 1], 100 * x[, 2]) [1] 80.10768 > cov(0.003 * x[, 1], 100 * x[, 2]) [1] 0.2403230 107
DRAFT
It is only when the two variances are equal to 1, as in the above variance-covariance matrix, that the covariance and the correlation are identical.
Now that we have seen how to create bivariate normal random numbers, we pro- ceed to estimate the corresponding density surface with the two-dimensional analogue ofdensity(), the functionkde2d(). The output ofkde2d()is a list withXcoordi- nates,Y coordinates, and theZ coordinate for each combination of theXandY. The number ofXcoordinates (andY coordinates) is specified with the parametern, which we set to50. Jointly, theX, Y andZcoordinates define the estimated density surface. We plot this surface withpersp(), which produces aPERSPECTIVE PLOT.
> persp(kde2d(x[, 1], x[, 2], n = 50),
+ phi = 30, theta = 20, # angles defining viewing direction + d = 10, # strength of perspective
+ col = "lightblue", # color for the surface
+ shade = 0.75, ltheta = -100, # shading for viewing direction + border = NA, # we use shading, so we disable border + expand = 0.5, # shrink the vertical direction by 0.5 + xlab = "X", ylab = "Y", zlab = "density") # add labels + mtext("bivariate standard normal", 3, 1) # and add title
The wide range of options ofpersp()is described in detail on its help page. You will also find the commanddemo(persp)useful, which gives some examples of whatpersp()
can do, including examples of the required code.
Paired vectors need not follow a bivariate normal distribution. The upper right panel of Figure 4.14 plots a bivariate density that isLOGNORMAL-POISSON DISTRIBUTED[cf. Baayen et al., 463–484]. This is a distribution that provides a reasonable first approxi- mation for paired word frequency counts obtained, e.g., by calculating the frequencies of a set of words in two equally sized text corpora. ALOGNORMAL RANDOM VARIABLE is a variate that is normally distributed after the logarithmic transformation. Given the (simplifying) assumption that word frequencies are lognormally distributed, we gener-
aten= 1000lognormally distributed random numbers withrlnorm()with which we
model the Poisson ratesλat which1000words are used in texts. In other words, for a given word, we model its token frequency in a text corpus as being Poisson-distributed. In order to simulate the frequency of a given word in two corpora, we generate two ran- dom numbers withrpois()for that word, given its usage rateλ.
Let’s make this more concrete by showing how this works inR. We begin with defining the number of wordsn, the corresponding vector of usage rateslambdas, and a two- column matrix of zeros in which we will store the two simulated frequencies of a given word.
> n = 1000 # number of words
> lambdas = rlnorm(n, 1, 4) # lognormal random numbers > mat = matrix(nrow = n, ncol = 2) # define matrix with zeros
We proceed with aFOR LOOPto store the two frequencies for each wordiinmat. The variableiin the loop starts at1, ends atn, and is incremented in steps of1. For each
DRAFT
value ofi, we fill thei-th row ofmatwith two Poisson random numbers, both obtained for the same Poisson rate given by thei-thλ.> for (i in 1:n) { # loop over each word index + mat[i,] = rpois(2, lambdas[i]) # store Poisson frequencies + } > mat[1:10,] [,1] [,2] [1,] 319 328 [2,] 22 18 [3,] 0 0 [4,] 3 2 [5,] 307 287 [6,] 29 29 [7,] 240 223 [8,] 2 1 [9,] 1 0 [10,] 523 527
The first row ofmatlists the frequencies for the first word, the second row those for the second word, and so on. Now thatmathas been properly filled with simulated frequen- cies of occurrence, we use it as input to the density estimation function. Before we do so, it is essential to apply a logarithmic transformation to remove most of the skew. As there are zero frequencies inmat, and as the logarithm of zero is undefined, we back off from zero by adding1to all cells ofmatbefore taking the log.
> mat = log(mat+1)
We now use the same code as previously for the bivariate normal density,
> persp(kde2d(mat[, 1], mat[, 2], n = 50),
+ phi = 30, theta = 20, d = 10, col = "lightblue",
+ shade = 0.75, box = T, border = NA, ltheta = -100, expand = 0.5, + xlab = "log X", ylab = "log Y", zlab = "density")
but change the accompanying text.
> mtext("bivariate lognormal-Poisson", 3, 1)
The lower panels of Figure 4.14 illustrate two empirical densities. The left panel con- cerns the phonological similarity space of4171Dutch word forms with four phonemes. For each of these words, we calculated the type count of four-phoneme words that differ in only one phoneme, its phonological neighborhood size. For each word, we also cal- culated the rank of that word in its neighborhood. (If the word was the most frequent word in its neighborhood, its rank was1, etc.) After removal of words with no neigh- bors and log transforms, we obtain a density that is clearly not strictly bivariate normal, but that might perhaps be considered as sufficiently approximating a bivariate normal distribution when considering a regression model.
109
DRAFT
The lower right panel of Figure 4.14 presents the density for the (log) frequencies of
4633Dutch monomorphemic noun stems in the singular and plural form. This distribu- tion has the same kind of shape as that of the lognormal-Poisson variate in the upper right.