I don't know if this is exactly what you want but if you want to set the groups manually, you have to put them in an array something like this:

    $groups = array('group1', 'group2', ...);
    
    foreach ($groups as $group) :
    ...


**EDIT:**

    $groups = $this->getGroups(); // your groups
    $mySelectedGroups = array('group1', 'group2', ...);

    foreach($group as $group) {
        if (in_array($group, $mySelectedGroups)) {
        //...
        }
    }

**EDIT 2** you have an error in your code


    <?php
    $groups = $this->getGroups();
    $mySelectedGroups = array('2', '4'); // this is the group entity_id from database
    ?>
    
    <!-- tab navigation -->
    <ul id="tabs" class="nav nav-tabs">
        <?php 
        $navI = 1;
        foreach ($groups as $group) :
            if (in_array($group, $mySelectedGroups)):
                if ($group->hasProducts()): ?>
                    <li <?php if ($navI == 1) { ?> class="active" <?php } ?> >
                        <a href="#<?php echo $group->getId(); ?>"
                           data-toggle="tab">
                            <?php echo $group->getName(); ?>
                            &nbsp;<i style="display: none;" class="text-danger error_glyph_<?php echo $group->getId(); ?> fa fa-exclamation-circle" aria-hidden="true"></i>
                        </a>
                    </li>
                    <?php $navI++; ?>
                <?php endif; ?>
            <?php endif; ?>
        <?php endforeach; ?>
    </ul>