-1

I am trying to write a basic code of simple linear regression with gradient descent method. here is my code.

linear = function(x,y,lr)
{
theta0 = 0

theta1 = 0

m=length(x)

hypo = theta0 +theta1*x

costt = cost(hypo , y)
prev_cost = 1000000

while (prev_cost > cost)
{

prev_cost = cost

theta0 = theta0 - (lr/m)*(hypo - y)

theta1 = theta1 - (lr/m)*(hypo - y)*x

hypo = theta0 + theta1*x

New_cost = cost(hypo , y)

if(New_cost < cost)
{
cost = New_cost
}
}
theta = c(theta0 , theta1)
return( theta )
}  

cost = function(hypo , y)
{
interm = (hypo - y)^2

interm1 = sum(interm)

interm2 = interm1/(2 * m)

return(interm2)  
}

but when I test it with data it generates a warning message.

There were 50 or more warnings (use warnings() to see the first 50)

and stop terminating. what is wrong in the code? when I used warnings I get

Warning messages:
1: In while (prev_cost > cost) { ... :
the condition has length > 1 and only the first element will be used 

lr = 0.01 which is learning rate. and this is the snap of data x and y enter image description here

Siddhant Vats
  • 41
  • 1
  • 4
  • 2
    please use `warnings()` and edit your question. We need to see the actual error message. Normally all warnings are the same – mischva11 Sep 21 '18 at 12:26
  • But not all warnings are created equally. – user2974951 Sep 21 '18 at 12:37
  • logic that i have used in the above code is right? @mischva11 – Siddhant Vats Sep 21 '18 at 12:44
  • the condition has length > 1 and only the first element will be used --this what I get when I used warnings – Siddhant Vats Sep 21 '18 at 12:45
  • `costt = cost(hypo , y)` - shouldn't it be `cost = cost(hypo , y)` ? Also, if the code has stopped running is there an Error? Please paste the output from `warnings()` – forestfanjoe Sep 21 '18 at 12:50
  • the condition has length > 1 and only the first element will be used --this what I get when I used warnings @forestfanjoe – Siddhant Vats Sep 21 '18 at 12:54
  • I don't think it should be cost not cost because then it could arise the problem of ambiguity in the code bcoz cost(hypo , y) is a function and costt is a value i am computing using that function – Siddhant Vats Sep 21 '18 at 12:57
  • In which case, I think you should change other instances of `cost` to to `costt` - eg - `while (prev_cost > cost)` – forestfanjoe Sep 21 '18 at 13:01
  • It would also be good to make available an example of x, y and lr - are these vectors of the same length? I can't reproduce the warnings/errors otherwise. – forestfanjoe Sep 21 '18 at 13:02
  • yes ur right i corrected it but still got the same warning message @forestfanjoe – Siddhant Vats Sep 21 '18 at 13:05
  • I think you need to add an `m` argument to your `cost` function. – forestfanjoe Sep 21 '18 at 13:32
  • Possible duplicate of [The condition has length > 1 and only the first element will be used](https://stackoverflow.com/questions/23316161/the-condition-has-length-1-and-only-the-first-element-will-be-used) – divibisan Sep 21 '18 at 22:52

1 Answers1

0

This runs for me. It produces a vector that is twice as long as length(x) - is this what you're after?

linear = function(x,y,lr)
{
    theta0 = 0

    theta1 = 0

    m=length(x)

    hypo = theta0 +theta1*x

    costt = cost(hypo , y, m)
    prev_cost = 1000000

    while (prev_cost > costt)
    {

        prev_cost = costt

        theta0 = theta0 - (lr/m)*(hypo - y)

        theta1 = theta1 - (lr/m)*(hypo - y)*x

        hypo = theta0 + theta1*x

        New_cost = cost(hypo , y, m)

        if(New_cost < costt)
        {
            costt = New_cost
        }
    }
    theta = c(theta0 , theta1)
    return( theta )
}  

cost = function(hypo , y, m)
{
    interm = (hypo - y)^2

    interm1 = sum(interm)

    interm2 = interm1/(2 * m)

    return(interm2)  
}

x <- rnorm(80)
y <- rnorm(80)
lr <- 0.01
linear(x, y, lr)
forestfanjoe
  • 412
  • 4
  • 11