- https://www.mathworks.com/help/stats/dummyvar.html
- https://www.mathworks.com/help/matlab/ref/categorical.html
Creating a dummy using experience parameter
2 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
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
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
0 Comments
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!