-1

In response to this question, I took upon the challenge to make my understanding on R's density() function.

Since I'm pretty much very new to R, I have no ideas about vectors regarding the c() function, which made me use a list as the closest form.

I would make this function:

def density(x, bw, adjust):
    bw2 = None
    result = 0
    if bw == "nrd0":
        bw2 = 31.39367
    else:
        print("No such bandwidth.")
    for i in range[len(x)]:
        x[i] = x[i] * bw2
    for i in range[len(x)]:
        result = result + x[i]
    return result * adjust

And I wanted to test it:

x = [1, 3, 5]
kern = density(x, "nrd0", 1)
print(kern)

And I gained 2 errors, the main one being a TypeError.

If you want to look into it further, here's the whole terminal message:

Traceback (most recent call last):
  File "density.py", line 15, in <module>
    kern = density(x, "nrd0", 1)
  File "density.py", line 8, in density
    for i in range[len(x)]:
TypeError: 'type' object is not subscriptable

How do I fix the TypeError?

1 Answers1

1
for i in range[len(x)]:
        x[i] = x[i] * bw2

You have range with [] while it should be (). Try to change it. Below is an example:

l = [10, 20, 30, 40] 
for i in range(len(l)): 
    print(l[i], end =" ") 
print()