Why do I talk to myself ?

Basically whenever I feel lonely I’ll pretend I’m with someone who I’m close with and have a conversation with them. I know they aren’t real but I make myself believe they are sometimes, why do I do this ?