필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
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!

채택된 답변

Rik
Rik 2020년 8월 18일
survival=cumprod(1-mortality);
  댓글 수: 3
Rik
Rik 2020년 8월 18일
%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 2020년 8월 18일
I figured that out, it should be,
survival = ones(size(age))
survival(2:end,:) = cumprod(1-mortality(1:end-1,:))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Industrial Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by