The DMG specifically says how to calculate CR. It says "include these factors". Including anything else is a houserule.
You calculate offensive CR (a function of DPR and hit bonuses/saves) and defensive CR (a function of HP and AC), and then you average the two. There's a finite list of things that modify the components of CR. And they can't give you specifics for anything else, because it all depends on the details.
Does hypnotic pattern deal damage? No? Then it doesn't affect CR because using it reduces the best-three-rounds DPR. Which is what is used.
42 HP: base defensive CR 1/4.
AC (assuming mage armor and +2-3 DEX): 15-16. That's 2-3 above par, so final defensive CR: 1/2.
Without further information, I can only assume that their offensive best three rounds are
1) lightning bolt (4) = 9d6 * 2 = 63
2) lightning bolt (4) = 9d6 * 2 = 63
3) lightning bolt (3) = 8d6 * 2 = 56
For a base offensive CR of 9. Level 7 ==> +4 INT ==> save DC = 15, which is one below the specified, so final offensive CR is unchanged = 9.
Total CR: 4.5 ==> 5 (rounding up due to damage output). So I was off a bit. But functionally, it acts like a really bity monster of much lower CR, because it will die if a PC sneezes on it.
Alone, this thing will die before it gets more than one attack off. 42 HP is trivial, even against a level 4 party.
Not to mention it's a bad monster, as are all such glass cannons. Because it comes down to "did it go first?" Then someone's going to be seriously hurt. Otherwise, it dies without putting up a fight at all. Likely won't even get a turn.
And in building this using PC rules, you've spent 5-10 minutes writing down a bunch of details you'll never use.
PCs and monsters have different design needs. They are best if they're built differently. Monsters can be built back-to-front, cherry picking the pieces you need. You both save time and make better fights that way. PCs have to have staying power over an entire day. And lots of moving bits to play with. Monsters don't need either, in fact those get in the way.