# Fitting a log (or generalized?) linear model

I would like to fit a model of the form

$z = k x^\alpha y^\beta$

to some data I have (it's a spatial gravity model). Now I know you could take logs of both sides and fit a linear regression

$\log z = \log k + \alpha \log x + \beta \log y$

But there are two problems:

1. Does this violate assumptions of OLS regression if $\log x$ and $\log y$ don't have a normal distribution?

2. Some of my $z$s are zero - how to deal with that?

EDIT

In response to whuber's question. This is a model of how many people from each of a number of districts travel to an event. $x$ is district population and $y$ is (mean) distance (from each district) - thus I expect $\alpha \approx 1$ and $\beta < 0$. $z$ is sampled from a survey of under 1% of over 100k visitors. I am interested in obtaining estimates for where the remaining 99% of visitors came from, for which I do not expect $z=0$ for any district, but in the limited sample data $z$ is sometimes zero because the survey people didn't happen to meet anybody from that district.

If anyone has comments about how the above modifies expectations about the error distribution on $z$ and hence the best way to fit the model I would be interested to hear it. As I was expecting $a \approx 1$, I tried nonlinear least squares with $a$ fixed at 1, but that led to extremely bad variance on $k$ ($\sigma_k > k$). I have since fitted GLM with a poisson link function which gives sensible $k$ and $\beta$ but $\alpha \approx 0.85$ - not great, but it's only a rough model so I can live with it.

• I see nothing "logistic" about any of this. Mar 12, 2014 at 15:24
• There's no assumption in OLS about the distribution on the predictors. If you have actual zeroes... don't take logs. Indeed even the possibility suggests taking logs is possibly not the way to go. Is the distribution of z more variable as the mean gets larger? Mar 13, 2014 at 7:13
• I changed the tags and took out the part about regressors being zero since you indicated that was not the case. Mar 14, 2014 at 6:14
• Good answers to this question would exploit information about the error structure of the $z$ data. Different kinds of data will benefit from different statistical treatments of this problem. The fact that some of the $z$s can be zero already indicates that taking logarithms will be inappropriate, but it doesn't really help decide among the many available alternatives. Could you perhaps edit your question to indicate what $z$ represents and how it is measured?
– whuber
Mar 14, 2014 at 15:14
• Very good! Your new information tells us a lot about how $z$ ought to behave and opens up the possibility of fitting (say) a generalized linear model of $z$ (not $\log(z)$!) in terms of $\log(x)$ and $\log(y)$ by using a suitable link function. Because $z$ is a survey response, a Binomial or Poisson model will be appropriate. I haven't time to provide details of this suggestion. For further information and a worked example of a GLM with a nonstandard link, please see my post at stats.stackexchange.com/a/64039.
– whuber
Mar 17, 2014 at 14:44

I would use a Poisson Quasi-MLE approach for a gravity model, especially with zeros in $z$. Code, intuition, and papers can be found at the Log of Gravity page.

• That sounds good advice to me. The problem of logging some predictor if zero presumably needs some dodge within that framework. Mar 12, 2014 at 17:13
• Indeed. The traditional explanatory variables are usually things like distance and GDPs of the two countries, which are usually positive. If $x$ is a dummy variable (like a trade agreement indicator), it can be modeled differently, like $\beta^x$ in the multiplicative framework, so you don't need to take logs of it. Mar 12, 2014 at 17:21
• None of the predictors are zero, just the target sometimes. Thanks :) Mar 13, 2014 at 10:51
• @SideshowBob Your question still says "some of my $x$ are zero". If that is not so, please correct it. Mar 13, 2014 at 11:55

How to do this in any software is off-topic here, but there are statistical questions that transcend software choice. You need to work out what to do before you seek the code to do it.

Question 1 is backwards. You can't fit your first form directly using standard regression functions or commands; you would need to turn to non-linear least squares. Both that and your second form imply assumptions about the error term, not the distributions of $x$, $y$, $\log x$ or $\log y$. As it happens, my wild guess with data like yours is that $\log y$ and $\log x$ will be closer to normal than the original variables; that does no harm, but it's not strictly relevant. The question is one for you, which is more realistic, the idea that errors are additive compared with $y$ or additive compared with $\log y$?

Question 2 is enormously difficult. Zeros in the response $z$ imply to me turning to a generalized linear model, but there is a serious argument that if zeros arise for structural reasons you may need a more complicated model in which extra zeros are modelled as such. There is no agreed answer on applying a logarithmic transformation of a predictor when there are zeros present, but this is much discussed elsewhere on this site, notably in this FAQ

I don't know the current literature here, but these problems are basic and should be prominently discussed in the better papers. If not, shame on the practitioners!

• Thanks. In response to the first question I would expect errors to be linear in $log x$ and $log y$. As to the zeros, I don't expect them to occur in practice, they come from the error term - i.e. they appear in our data because of limited sampling. Mar 12, 2014 at 11:43
• If using a GLM, does the presence of zeros affect the choice of link function? Mar 12, 2014 at 11:43
• If you expect errors to vary with logged predictors, OLS can't be right! I am trying to decode your statement about zeros, but it sounds as if you don't expect structural zeros, i.e. zeros that are inevitable given circumstances. Zeros in the response are consistent with just about any link you might entertain here; if there are too many of them, that might imply that you are fitting an inappropriate model. Much of this stuff is first or second course linear models, or should be. Mar 12, 2014 at 11:57
• Thanks. If this is basic stuff can you recommend a good textbook please? clearly it's a gap in my knowledge! Mar 12, 2014 at 12:50
• Depends on your background. If it's economics, then a mainstream econometrics book. If it's not, you need a modern regression text (too many to choose from) followed by a generalised linear models text (start with Dobson and Barnett, not McCullagh and Nelder). But don't expect any of them to start talking about spatial gravity models as an example; for that you may need journal literature. Mar 12, 2014 at 12:54