Well, here's my opinion. It's not based on anything other than what I think. I don't have any research to back up my theories.
An imaginary friend is really a part of you. It's almost like a dissociation from your conscience. Think of a kid just playing with his/her toys. The kid is gonna talk to them, or pretend they say things. An imaginary friend is like that, except that there are no toys present.
When I first came to the US, I didn't have many friends and was often lonely. The language barrier made it somewhat difficult. So I befriended Leonardo from the TMNT series. I pretended like he could open a portal and visit me in my world, and that we could be friends. I walked to elementary school, about 2 miles each way, and talked to him. After I started making friends, I started to imagine him less and less.
If you've ever written a diary, there's a chance that you might have been writing as if you were addressing someone else. People will do that, Dear Diary... It's not that far a stretch to come up with an imaginary entity that would listen to you.
Some folks just talk to their imaginary friends. Others go to the extent of putting words in their imaginary mouths. There's nothing strange about it. It's the same as talking to yourself. Because that's where the "friend" came from.
People who have sexual fantasies about people, real or imagined, are in fact creating temporary imaginary friends (with benefits).
I don't think it's a problem, unless the friends start splitting off and talking back uncontrollably. This could be a sign that you're not being entirely honest with yourself (which is a good thing to keep in mind), or it could be an onset of schizophrenia, which can reach maturity as late as someone's early 20s (if you want to believe the textbooks). Or even of dissociative identity disorder (FKA multiple personality disorder).
It's interesting that you mention this. Since I've become pretty much a recluse, I've considered creating an imaginary friend. I compulsively entertain people. My social anxiety is really bad sometimes. So I figured, what would be the harm of setting up a different e-mail account or writing snail mail letters to myself, for the sake of company. Company that I could actually enjoy.
Then I realized that doing so would just further isolate me from social interactions and dissociate me from reality. So I talked to my fiance instead! And he agreed to spend more time with me. He's making me crab cakes this Saturday.
So my plans for an imaginary friend are currently on hold, lol!

And I'm dealing with social anxiety issues in a different and healthier way. There's a difference between being self-sufficient and imagining someone who fills the place of a real person.
There are worse ways to cope with loneliness, though, than having an imaginary friend.
Do not take my advice before talking to your doctor/counselor/other professional. Depending on where you live, you may be able to find free, confidential care. Most importantly, sometimes your shrink can be wrong. Get a second opinion.