2
$\begingroup$

I'm trying to write some code to do a regression on data weight (x) and time (y). As best as I can tell, the model should be y = b1 + b2ln(x), but I don't know how you can do this by hand (I know how to in R...). I know how to do a simple linear regression by hand. Appreciate it.

$\endgroup$
10
  • $\begingroup$ calculate logs: x1 = ln(x). Regress y on x1. Done. BTW your model is wrong unless your errors are all 0 (in which case you can just compute it from any two distinct points). That is, you either need to write $E(y)=$ or you need to add $+e$ on the end $\endgroup$
    – Glen_b
    Commented Dec 2, 2013 at 3:16
  • $\begingroup$ can it be done using the least squares approach? $\endgroup$
    – Collin
    Commented Dec 2, 2013 at 3:29
  • $\begingroup$ That's what I'm saying up there, yes. How are you doing it in R if not by least squares? $\endgroup$
    – Glen_b
    Commented Dec 2, 2013 at 3:47
  • $\begingroup$ I guess I'm not exactly sure lol. So normally you would calculate SXX = sum(x-xbar)^2; SXY = sum((x-xbar)(y-ybar)); SYY = sum(y-ybar)^2. Then B1 = SXY/SXX. How would this differ given the log term? $\endgroup$
    – Collin
    Commented Dec 2, 2013 at 3:58
  • $\begingroup$ The entire thing is in my first comment. Try reading it again, please. It has two steps. Tell me what you don't understand about step 1 (Calculate the logs, calling the result $x_1$). If you understand what that means, we can move to step 2. $\endgroup$
    – Glen_b
    Commented Dec 2, 2013 at 4:20

1 Answer 1

4
$\begingroup$

You say that you'd calculate the slope as follows:

So normally you would calculate

$$S_{XX} = \sum_i (x_i-\bar x)^2\\ S_{XY} = \sum_i ((x_i-\bar x)(y_i-\bar y))\\ S_{YY} = \sum(y_i-\bar y)^2$$

Then $b_2 = S_{XY}/S_{XX}$.

So imagine you have a set of x-values and y-values:

    y       x
1 2.3 0.36772
2 5.3 1.64873
3 6.5 7.38910

Step 1:

calculate a new $x$, $x_1 = \ln(x)$

    y   x1
1 2.3 -1.0
2 5.3  0.5
3 6.5  2.0

Now regress $y$ on this new $x_1$ as usual

 SXX = 4.50; SXY= 6.30; SYY = 9.36

 b2 = SXY/SXX = 1.4

 b1 = mean(y) - b2 . mean(x) = 4.7 - 1.4 . 0.5 = 4

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.