Borrar filtros
Borrar filtros

create a new variable that is the product of lag value of another variable and its own lag value

4 visualizaciones (últimos 30 días)
my two input variables are age and mortality,
I want to create another variable "survival = 1" for first value of "age" like here first value of age = 65, but it can be any age
then for following ages 66, 67, 68 onwards, it should take the value from previous survival value at age 65 and mutliply with corresponding (1-mortality).
for example, my two input variables are age and mortality,
age mortality
65 0.0347029096019856
66 0.0382583912888586
67 0.0421156045159524
68 0.0463212051920102
69 0.0509158865323249
70 0.0559879627737633
my output variable is survival
age mortality survival
65 0.0347 1
66 0.0382 1*(1- 0.0347) = 0.9653
67 0.0421 0.9653 *(1-0.0382) = 0.9284
68 0.0463 0.9284*(1-0.0421) = 0.8898
.
.
.
.
This is the code which I had written but it is giving wrong values. I do not know whether I should for-loop or is there an easy way out.
survival = ones(size(age))
for i = 2 : length(???)
survival = survival(i-1,:).*(1-mortality(1:end-1,:))
end
I would appreciate your help!

Respuesta aceptada

Rik
Rik el 18 de Ag. de 2020
survival=cumprod(1-mortality);
  3 comentarios
Rik
Rik el 18 de Ag. de 2020
%you can do it the long way round:
survival=cat(1+(size(mortality,1)<size(mortality,2)),1,cumprod(1-mortality(1:(end-1))));
%or pick the correct one from these:
survival=[1;cumprod(1-mortality(1:(end-1))))];
survival=[1,cumprod(1-mortality(1:(end-1))))];
susman
susman el 18 de Ag. de 2020
I figured that out, it should be,
survival = ones(size(age))
survival(2:end,:) = cumprod(1-mortality(1:end-1,:))

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by