I'm searching for an answer for the following problem:
I want to create a numpy array where all the intercepts and slopes are stored. The slope is the increase of means
over the years
. I have found multiple ways to calculate the intercept/slope, but I really miss the link to get them in a new array (I'm new to Numpy so the logic is slowly getting there but I've now been stuck for a day..)
So.. I have an array that is structured like this:
x = np.array([(2000, 'A', '1',5), (2001, 'A', '1', 10),
(2003, 'A', '1',15), (2004, 'A', '1', 20),
(2000, 'A', '2',1), (2001, 'A', '2', 2),
(2002, 'A', '2', 3), (2003, 'A', '2', 4)],
dtype=[('year', 'i4'), ('group1', 'U2'), ('group2', 'U2'), ('means', 'i2')])
And I would like to end up with an array like this:
>desired_array
array([('A', '1', 5, 5),
('A', '2', 1, 1)],
dtype=[('group1', '<U2'), ('group2', '<U2'), ('intercept', '<i2'), ('slope', '<i2')])
I have gotten to this point:
ans, indices = np.unique(x[['group1', 'group2']], return_inverse=True)
desired_array = np.empty(2, dtype=[('group1', 'U2'), ('group2', 'U2'), ('intercept', '<f8'),
('slope', '<f8')])
desired_array['group1'] = ans['group1']
desired_array['group2'] = ans['group2']
x = x[x['year'] == 2000]
desired_array['intercept'] = x['means']
it's a bit rough which I can still improve but the main question for me where I get stuck is how to add the slope per regression line to the array.
Would be great is someone could help me out :)