Creating a dummy using experience parameter

10 ビュー (過去 30 日間)
Ruben Moreno
Ruben Moreno 2022 年 5 月 12 日
編集済み: Gowtham 2023 年 9 月 27 日
I have a large dataset consisiting of several financial deals, with a variable of investors in the deal. I have counted the occurance of every investor, and want to create a dummy variable in each deal depening on they have experience or not. Experienced being deemed as above mean. The dataset is quite large, so will need a loop of some kind i think. Added a picture for visualization of investor representation

回答 (1 件)

Gowtham
Gowtham 2023 年 9 月 25 日
編集済み: Gowtham 2023 年 9 月 27 日
Hello Ruben,
I understand that you want to create a dummyvar given the conditions on experience in the given data.
Considering the data is provided with the columns ‘names’, ‘count’, ‘mean’ and ‘observations_above_mean’ in a ‘table’ format, we can use logical indexing instead of loops to perform the task.
For a sample demonstration of the above process, kindly refer to the following code snippet:
% Create a sample table data
names = {'Investor A'; 'Investor B'; 'Investor C'; 'Investor D'; 'Investor E'};
count = [8; 10; 4; 6; 12];
mean = [7; 7; 7; 7; 7];
observations_above_mean = [1; 3; 0; 2; 5];
% Create the table
data = table(names, count, mean, observations_above_mean);
% Set the threshold for observations above mean
threshold = 2; % Adjust the threshold value as needed
% Create a logical vector indicating whether each investor has experience or not
experience = data.observations_above_mean > threshold;
% Convert the logical vector to a categorical variable
experience_category = categorical(experience, [0 1], {'No', 'Yes'});
% Use the dummyvar function to create dummy variables based on the categorical variable
dummy_variables = dummyvar(experience_category);
% Concatenate the original table with the dummy variables
data_with_dummies = [data array2table(dummy_variables)];
% Rename the target columns
data_with_dummies = renamevars(data_with_dummies, ["dummy_variables1", "dummy_variables2"], ["not_experienced", "experienced"]);
% Display the resulting table
data_with_dummies
data_with_dummies = 5×6 table
names count mean observations_above_mean not_experienced experienced ______________ _____ ____ _______________________ _______________ ___________ {'Investor A'} 8 7 1 1 0 {'Investor B'} 10 7 3 0 1 {'Investor C'} 4 7 0 1 0 {'Investor D'} 6 7 2 1 0 {'Investor E'} 12 7 5 0 1
The provided output is the result of running the code snippet, which includes the columns labelled as not_experienced and experienced.
Kindly refer to the following MATLAB documentation for further understanding on dummyvar and categorical functions
Hope this helps.
Best Regards,
Gowtham

カテゴリ

Help Center および File ExchangeAnalysis of Variance and Covariance についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by