Creating a dummy using experience parameter

2 views (last 30 days)
Ruben Moreno
Ruben Moreno on 12 May 2022
Edited: Gowtham on 27 Sep 2023
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

Answers (1)

Gowtham
Gowtham on 25 Sep 2023
Edited: Gowtham on 27 Sep 2023
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

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!